sp_ListFields.sql 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. -- LIST TABLES IN A DATABASE OR LIST FIELDS IN A TABLE.
  2. -- ALSO LISTS TABLE DESCRIPTIONS AND FIELD DESCRIPTIONS.
  3. -- TO ADD A TABLE DESCRIPTION, ADD AN EXTENDED PROPERTY TO IT.
  4. -- TO ADD A FIELD DESCRIPTION, ADD A DESCRIPTION TO THE FIELD
  5. -- WHEN DESIGNING THE TABLE.
  6. CREATE PROCEDURE [dbo].[sp_ListFields]
  7. @TableName SYSNAME = NULL
  8. AS
  9. BEGIN
  10. -- =============================================
  11. -- Create date: 1/1/2009
  12. -- Description: List fields in a database with the comments.
  13. -- =============================================
  14. DECLARE @iTableID INT
  15. DECLARE @ForeignKeys TABLE
  16. (
  17. RowID INT IDENTITY(1,1),
  18. PrimaryTableName SYSNAME,
  19. PrimaryFieldName SYSNAME,
  20. ForeignTableName SYSNAME,
  21. ForeignFieldName SYSNAME,
  22. PrimaryColID INT,
  23. ForeignColID INT,
  24. PrimaryTableID INT,
  25. ForeignTableID INT
  26. )
  27. DECLARE @ForeignKeyOutput TABLE
  28. (
  29. ColID INT,
  30. HasForeignKeysAt NVARCHAR(4000)
  31. )
  32. DECLARE @PrimaryKeyOutput TABLE
  33. (
  34. ColID INT,
  35. HasPrimaryKeyAt NVARCHAR(4000)
  36. )
  37. SET NOCOUNT ON
  38. DECLARE @iRowLoop INT
  39. DECLARE @iRowCount INT
  40. DECLARE @sKeyTableName SYSNAME
  41. DECLARE @sKeyFieldName SYSNAME
  42. DECLARE @iKeyColID INT
  43. DECLARE @iKeyTableID INT
  44. DECLARE @sForeignKeyInfo NVARCHAR(4000)
  45. IF ISNULL(@TableName,'') <> ''
  46. BEGIN
  47. SELECT @iTableID = id
  48. FROM sysobjects
  49. WHERE [name] = @TableName
  50. AND xtype='u'
  51. END
  52. IF ISNULL(@iTableID,0) = 0
  53. BEGIN
  54. -- LIST TABLE
  55. SELECT SO.[name] AS TableName
  56. ,ISNULL(SP.value,'') as Description
  57. from sysobjects AS SO
  58. LEFT OUTER JOIN sys.extended_properties as SP
  59. ON SO.id = SP.major_id
  60. AND SP.minor_id=0
  61. where SO.xtype='u'
  62. order by SO.name
  63. END ELSE
  64. BEGIN
  65. -- GET THE FOREIGN KEYS
  66. INSERT INTO @ForeignKeys (PrimaryTableName,PrimaryFieldName
  67. ,ForeignTableName,ForeignFieldName
  68. ,PrimaryColID,ForeignColID
  69. ,PrimaryTableID,ForeignTableID)
  70. SELECT PT.name as PrimaryTableName
  71. ,cp.name as PrimaryFieldName
  72. ,FT.name as ForeignTableName
  73. ,cf.NAME AS ForeignFieldName
  74. ,K.rkey AS PColID
  75. ,K.fkey AS FColID
  76. ,K.rkeyid as PrimaryTableID
  77. ,K.fkeyid as ForeignTableID
  78. FROM sysforeignkeys AS K
  79. INNER JOIN sysobjects AS PT
  80. ON K.rkeyid = PT.id
  81. INNER JOIN sysobjects AS FT
  82. ON K.fkeyid = FT.id
  83. INNER JOIN syscolumns AS CP
  84. ON K.rkeyid = CP.id AND K.rkey = CP.colid
  85. INNER JOIN syscolumns AS CF
  86. ON K.fkeyid = CF.id AND K.fkey = CF.colid
  87. WHERE K.fkeyid = @iTableID
  88. OR K.rkeyid = @iTableID
  89. -- LOOP THROUGH THE FOREIGN KEYS.
  90. SELECT @iRowCount = MAX(RowID) FROM @ForeignKeys
  91. -- FIND THE FIELDS THAT ARE PRIMARY KEYS.
  92. SET @iRowLoop = 1
  93. WHILE @iRowLoop <= @iRowCount
  94. BEGIN
  95. SELECT @sKeyTableName = ForeignTableName
  96. ,@sKeyFieldName = ForeignFieldName
  97. ,@iKeyColID = PrimaryColID
  98. ,@iKeyTableID = PrimaryTableID
  99. FROM @ForeignKeys
  100. WHERE RowID = @iRowLoop
  101. IF @iKeyTableID = @iTableID
  102. BEGIN
  103. -- THIS FIELD IS A PRIMARY KEY TO SOME FOREIGN KEY IN A DIFFERENT TABLE.
  104. SET @sForeignKeyInfo = NULL
  105. SELECT @sForeignKeyInfo = HasForeignKeysAt
  106. FROM @ForeignKeyOutput
  107. WHERE ColID = @iKeyColID
  108. SET @sForeignKeyInfo = ISNULL(@sForeignKeyInfo,'')
  109. IF LEN(@sForeignKeyInfo) > 0
  110. BEGIN
  111. SET @sForeignKeyInfo = @sForeignKeyInfo + N','
  112. END
  113. SET @sForeignKeyInfo = @sForeignKeyInfo + @sKeyTableName + N'.' + @sKeyFieldName
  114. DELETE FROM @ForeignKeyOutput
  115. WHERE ColID = @iKeyColID
  116. INSERT INTO @ForeignKeyOutput (ColID,HasForeignKeysAt)
  117. VALUES (@iKeyColID,@sForeignKeyInfo)
  118. END
  119. SET @iRowLoop = @iRowLoop + 1
  120. END
  121. -- FIND THE FIELDS THAT ARE FOREIGN KEYS.
  122. SET @iRowLoop = 1
  123. WHILE @iRowLoop <= @iRowCount
  124. BEGIN
  125. SELECT @sKeyTableName = PrimaryTableName
  126. ,@sKeyFieldName = PrimaryFieldName
  127. ,@iKeyColID = ForeignColID
  128. ,@iKeyTableID = ForeignTableID
  129. FROM @ForeignKeys
  130. WHERE RowID = @iRowLoop
  131. IF @iKeyTableID = @iTableID
  132. BEGIN
  133. -- THIS FIELD IS A PRIMARY KEY TO SOME FOREIGN KEY IN A DIFFERENT TABLE.
  134. SET @sForeignKeyInfo = NULL
  135. SELECT @sForeignKeyInfo = HasPrimaryKeyAt
  136. FROM @PrimaryKeyOutput
  137. WHERE ColID = @iKeyColID
  138. SET @sForeignKeyInfo = ISNULL(@sForeignKeyInfo,'')
  139. IF LEN(@sForeignKeyInfo) > 0
  140. BEGIN
  141. SET @sForeignKeyInfo = @sForeignKeyInfo + N','
  142. END
  143. SET @sForeignKeyInfo = @sForeignKeyInfo + @sKeyTableName + N'.' + @sKeyFieldName
  144. DELETE FROM @PrimaryKeyOutput
  145. WHERE ColID = @iKeyColID
  146. INSERT INTO @PrimaryKeyOutput (ColID,HasPrimaryKeyAt)
  147. VALUES (@iKeyColID,@sForeignKeyInfo)
  148. END
  149. SET @iRowLoop = @iRowLoop + 1
  150. END
  151. -- LIST FIELDS IN THE TABLE.
  152. SELECT SC.name as [Column Name]
  153. ,ST.name AS [Data Type]
  154. ,SC.length as [Character Maximum Length]
  155. ,SC.isnullable as [Is Nullable]
  156. ,ISNULL(SP.value,'') As [Column Description]
  157. ,ISNULL(SCO.[text],'') as [Column Default]
  158. ,ISNULL(PKO.HasPrimaryKeyAt,'') As [Has Primary Key At]
  159. ,ISNULL(FKO.HasForeignKeysAt,'') AS [Has Foreign Keys At]
  160. FROM syscolumns AS SC
  161. INNER JOIN systypes AS ST
  162. ON SC.xtype = ST.xtype
  163. LEFT OUTER JOIN @ForeignKeyOutput AS FKO
  164. ON SC.colid = FKO.colid
  165. LEFT OUTER JOIN @PrimaryKeyOutput AS PKO
  166. ON SC.colid = PKO.colid
  167. LEFT OUTER JOIN syscomments as SCO
  168. ON SC.cdefault = SCO.id
  169. LEFT OUTER JOIN sys.extended_properties as SP
  170. ON SC.colid = SP.minor_id
  171. AND SC.id = SP.major_id
  172. AND SP.name = 'MS_Description'
  173. WHERE ST.name <> 'sysname'
  174. and SC.id = @iTableID
  175. ORDER BY SC.colid
  176. END
  177. END