csp_DropColumnDescriptions.txt 4.0 KB

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