csp_ExportColumnDescriptions.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE PROCEDURE dbo.csp_ExportColumnDescriptions
  6. @TableName SYSNAME -- Can be in the format like 'Invoices' or 'dbo.invoices' or '[invoices]' or '[RH8].[invoices]'
  7. AS
  8. /******************************************************************************
  9. ** File:
  10. ** Name: csp_ExportColumnDescriptions
  11. ** Desc: Exports the column descriptions for a specified table in a format
  12. ** that's good for importing to another database.
  13. *******************************************************************************/
  14. BEGIN
  15. DECLARE @Items TABLE
  16. (
  17. ItemID INT IDENTITY(1,1),
  18. Item NVARCHAR(200),
  19. ItemType VARCHAR(50),
  20. PRIMARY KEY(itemID)
  21. );
  22. DECLARE @Export TABLE
  23. (
  24. RowID INT IDENTITY(1,1),
  25. ColumnName SYSNAME,
  26. [Description] NVARCHAR(4000),
  27. PRIMARY KEY(RowID)
  28. );
  29. DECLARE @Output TABLE
  30. (
  31. RowID INT IDENTITY(1,1),
  32. [SQL] NVARCHAR(4000),
  33. PRIMARY KEY(RowID)
  34. );
  35. DECLARE @Length INT;
  36. DECLARE @Loop INT;
  37. DECLARE @Start INT;
  38. DECLARE @ItemCount INT;
  39. DECLARE @Ch VARCHAR(1);
  40. DECLARE @InSquareBrackets INT;
  41. DECLARE @SQL NVARCHAR(4000);
  42. DECLARE @Schema NVARCHAR(200);
  43. DECLARE @Table NVARCHAR(200);
  44. DECLARE @Column NVARCHAR(200);
  45. DECLARE @TableID INT;
  46. DECLARE @Description NVARCHAR(4000);
  47. -- Parse @Field by . and put it im @Items.
  48. SET @Length = LEN(@TableName);
  49. SET @Start = 1;
  50. SET @Loop = 1;
  51. SET @InSquareBrackets = 0;
  52. WHILE @Loop <= @Length
  53. BEGIN
  54. SET @Ch = SUBSTRING(@TableName,@Loop,1);
  55. IF @Ch = '['
  56. BEGIN
  57. SET @InSquareBrackets = @InSquareBrackets + 1;
  58. IF @InSquareBrackets > 1
  59. BEGIN
  60. THROW 50000,'Square brackets incorrect in @Field',1;
  61. END;
  62. END;
  63. IF @Ch = ']'
  64. BEGIN
  65. SET @InSquareBrackets = @InSquareBrackets - 1;
  66. IF @InSquareBrackets < 0
  67. BEGIN
  68. THROW 50000,'Square brackets incorrect in @Field',1;
  69. END;
  70. END;
  71. IF @Ch = '.' AND @InSquareBrackets = 0
  72. BEGIN
  73. INSERT INTO @Items (Item,ItemType)
  74. VALUES(SUBSTRING(@TableName,@Start,@Loop-@Start),'');
  75. SET @Start = @Loop + 1;
  76. END
  77. SET @Loop = @Loop + 1;
  78. END;
  79. INSERT INTO @Items (Item,ItemType)
  80. VALUES (SUBSTRING(@TableName,@Start,@Length-@Start+1),'');
  81. -- Remove [ and ] from @Items.Item
  82. UPDATE @Items
  83. SET Item = SUBSTRING(Item,2,LEN(Item)-2)
  84. WHERE LEN(Item) >=2 AND SUBSTRING(Item,1,1) = '[' AND SUBSTRING(Item,LEN(Item),1) = ']';
  85. -- Add descriptions to Item.
  86. SELECT @ItemCount = COUNT(*) FROM @Items;
  87. IF @ItemCount < 1 OR @ItemCount > 2
  88. BEGIN
  89. SET @SQL = N'Incorrect number of items in @TableName';
  90. THROW 50000,@SQL,1;
  91. END;
  92. IF @ItemCount = 2
  93. BEGIN
  94. UPDATE @Items SET ItemType = CASE ItemID WHEN 1 THEN 'Schema' WHEN 2 THEN 'Table' END;
  95. END
  96. IF @ItemCount = 1
  97. BEGIN
  98. UPDATE @Items SET ItemType = CASE ItemID WHEN 1 THEN 'Table' END;
  99. END
  100. -- Find the TableID.
  101. SELECT @Schema = Item FROM @Items WHERE ItemType = 'Schema';
  102. SELECT @Table = Item FROM @Items WHERE ItemType = 'Table';
  103. -- Find out how many tables have this name.
  104. SELECT @TableID = COUNT(*) FROM sys.tables WHERE [type] = 'U' AND name = @Table;
  105. IF ISNULL(@TableID,0) = 0
  106. BEGIN
  107. SET @SQL = CONCAT(N'Table ',@Table,N' wasn`t found');
  108. THROW 50000,@SQL,1;
  109. END
  110. IF ISNULL(@TableID,0) = 1
  111. BEGIN
  112. -- Don't use schema.
  113. SET @TableID = 0;
  114. SELECT @TableID = object_id,@Schema = SCHEMA_NAME(schema_id) FROM sys.tables WHERE [type] = 'U' AND name = @Table;
  115. END ELSE
  116. BEGIN
  117. -- Use schema.
  118. SELECT @TableID = object_id FROM sys.tables WHERE [type] = 'U' AND name = @Table AND SCHEMA_NAME(schema_id) = @Schema;
  119. END
  120. IF ISNULL(@TableID,0) = 0
  121. BEGIN
  122. SET @SQL = CONCAT(N'Table ',@Table,N' wasn`t found');
  123. THROW 50000,@SQL,1;
  124. END;
  125. INSERT INTO @Export (ColumnName, [Description])
  126. SELECT SC.name AS ColumnName,CONVERT(NVARCHAR(4000),SEP.value) as [Description]
  127. FROM sys.extended_properties AS SEP
  128. INNER JOIN sys.columns as SC
  129. ON SEP.minor_id = SC.column_id
  130. WHERE
  131. SEP.class_desc = 'OBJECT_OR_COLUMN'
  132. AND SEP.NAME = 'Ms_Description'
  133. AND SEP.major_id = @TableID
  134. AND SC.object_id = @TableID
  135. ORDER BY SC.name;
  136. SET @Length = @@ROWCOUNT;
  137. SET @Loop = 1;
  138. WHILE @Loop <= @Length
  139. BEGIN
  140. SELECT @Column = ColumnName,@Description = [Description]
  141. FROM @Export
  142. WHERE RowID = @Loop;
  143. INSERT INTO @Output ([SQL]) VALUES (CONCAT(N'EXEC sys.sp_addextendedproperty @name=N''MS_Description'', @value=N''',REPLACE(@Description,N'''',N''''''),N''''));
  144. INSERT INTO @Output ([SQL]) VALUES (CONCAT(N' , @level0type=N''SCHEMA'',@level0name=N''',REPLACE(@Schema,N'''',N''''''),N''''));
  145. INSERT INTO @Output ([SQL]) VALUES (CONCAT(N' , @level1type=N''TABLE'',@level1name=N''',REPLACE(@Table,N'''',N''''''),N''''));
  146. INSERT INTO @Output ([SQL]) VALUES (CONCAT(N' , @level2type=N''COLUMN'',@level2name=N''',REPLACE(@Column,N'''',N''''''),N''';'));
  147. INSERT INTO @Output ([SQL]) VALUES ('');
  148. SET @Loop = @Loop + 1;
  149. END;
  150. SELECT [SQL]
  151. FROM @Output
  152. ORDER BY RowID;
  153. END