stringscript.sql
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:4k
源码类别:

其他游戏

开发平台:

Python

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_String_StringType]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
  2. ALTER TABLE [dbo].[String] DROP CONSTRAINT FK_String_StringType
  3. GO
  4. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddStringEntry]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  5. drop procedure [dbo].[AddStringEntry]
  6. GO
  7. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StringContents]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  8. drop procedure [dbo].[StringContents]
  9. GO
  10. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AnimationString]') and OBJECTPROPERTY(id, N'IsView') = 1)
  11. drop view [dbo].[AnimationString]
  12. GO
  13. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[String]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  14. drop table [dbo].[String]
  15. GO
  16. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StringType]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  17. drop table [dbo].[StringType]
  18. GO
  19. CREATE TABLE [dbo].[String] (
  20. [StringId] [int] IDENTITY (1, 1) NOT NULL ,
  21. [StringValue] [varchar] (255) NOT NULL ,
  22. [StringTypeId] [smallint] NOT NULL 
  23. ) ON [PRIMARY]
  24. GO
  25. CREATE TABLE [dbo].[StringType] (
  26. [StringTypeId] [smallint] IDENTITY (1, 1) NOT NULL ,
  27. [StringTypeDesc] [varchar] (50) NOT NULL 
  28. ) ON [PRIMARY]
  29. GO
  30. ALTER TABLE [dbo].[String] WITH NOCHECK ADD 
  31. CONSTRAINT [PK_String] PRIMARY KEY  CLUSTERED 
  32. (
  33. [StringId]
  34. )  ON [PRIMARY] 
  35. GO
  36. ALTER TABLE [dbo].[StringType] WITH NOCHECK ADD 
  37. CONSTRAINT [PK_StringType] PRIMARY KEY  CLUSTERED 
  38. (
  39. [StringTypeId]
  40. )  ON [PRIMARY] 
  41. GO
  42. ALTER TABLE [dbo].[String] WITH NOCHECK ADD 
  43. CONSTRAINT [IX_String] UNIQUE  NONCLUSTERED 
  44. (
  45. [StringValue]
  46. )  ON [PRIMARY] 
  47. GO
  48. ALTER TABLE [dbo].[String] ADD 
  49. CONSTRAINT [FK_String_StringType] FOREIGN KEY 
  50. (
  51. [StringTypeId]
  52. ) REFERENCES [dbo].[StringType] (
  53. [StringTypeId]
  54. )
  55. GO
  56. SET QUOTED_IDENTIFIER ON 
  57. GO
  58. SET ANSI_NULLS ON 
  59. GO
  60. -- An example view that would be used to create what appears to be a physical table representing
  61. -- all the animation strings within the String table.  This would be useful when doing data entry to
  62. -- present the list of value animations to choose from.
  63. CREATE VIEW dbo.AnimationString
  64. AS
  65.   SELECT     StringId, StringValue
  66.   FROM         dbo.String
  67.   WHERE     (StringTypeId = 1)
  68. GO
  69. SET QUOTED_IDENTIFIER OFF 
  70. GO
  71. SET ANSI_NULLS ON 
  72. GO
  73. SET QUOTED_IDENTIFIER ON 
  74. GO
  75. SET ANSI_NULLS OFF 
  76. GO
  77. -- Adds a new entry to the String Table, by string type
  78. CREATE PROCEDURE AddStringEntry(@stringValue varchar(255), @stringType int)
  79. AS
  80.   SET NOCOUNT ON
  81.  
  82.   DECLARE @stringId int, @existingStringType smallint, @error int
  83.   -- Check if the string already exists, if it does, return the existing value
  84.   SELECT @stringId = StringId , @existingStringType = StringTypeId
  85.   FROM String
  86.   WHERE StringValue = @stringValue
  87.   
  88.   IF @@RowCount = 1
  89.   BEGIN
  90.     IF @existingStringType = @stringType
  91.       BEGIN
  92.         SELECT @stringId AS StringId
  93.         RETURN
  94.       END
  95.     ELSE
  96.       BEGIN
  97.         RAISERROR('The specified sting exists, but as a different type than the one passed in!', 16, 1)
  98.         SELECT 0
  99.         RETURN
  100.       END
  101.   END
  102.   
  103.   -- The string does not exist, add it
  104.   INSERT 
  105.   INTO String
  106.   VALUES (@stringValue, @stringType)
  107.   
  108.   -- Capture the value assigned by the database for the string id
  109.   SET @error = @@ERROR  
  110.   SET @stringId = @@IDENTITY
  111.   
  112.   -- If there was an error in the INSERT of the row, exit and return
  113.   -- 0 as the id, meaning one was not created
  114.   IF @error <> 0
  115.   BEGIN
  116.     SELECT 0 AS StringId
  117.     RETURN
  118.   END
  119.   
  120.   -- Everything went ok, so return the value of id of the newly added string
  121.   SELECT @stringId AS StringId
  122. GO
  123. SET QUOTED_IDENTIFIER OFF 
  124. GO
  125. SET ANSI_NULLS ON 
  126. GO
  127. SET QUOTED_IDENTIFIER ON 
  128. GO
  129. SET ANSI_NULLS OFF 
  130. GO
  131. -- Returns the entire contents of the string table for the purposes of building the lookups.  Note we
  132. -- don't care about the type at this point, the string Ids are guaranteed to be unique, and thus have
  133. -- no overlap
  134. CREATE PROCEDURE StringContents
  135. AS
  136.   SELECT StringId, StringValue
  137.   FROM String
  138. GO
  139. SET QUOTED_IDENTIFIER OFF 
  140. GO
  141. SET ANSI_NULLS ON 
  142. GO