1. 程式人生 > 實用技巧 >刪除SqlServer資料表和列的描述

刪除SqlServer資料表和列的描述

最近專案有部署到私有云的計劃。為了最後的尊嚴,不能把完整的資料庫給對方反向工程了。需要把資料表和列的描述給刪掉。

直接上程式碼,用的遊標處理方式。

 1 --表描述
 2 DECLARE @tn NVARCHAR(50)
 3 DECLARE c CURSOR FOR SELECT t.name FROM sys.tables AS t INNER JOIN sys.extended_properties AS p ON t.[object_id]=p.major_id WHERE t.[type]='U' AND p.minor_id = 0 AND p.[name] = N'MS_Description
' ORDER BY t.name 4 OPEN c 5 FETCH NEXT FROM c INTO @tn 6 WHILE @@FETCH_STATUS=0 7 BEGIN 8 EXEC sys.sp_dropextendedproperty @name = N'MS_Description' ,@level0type = N'SCHEMA', @level0name = N'gxjt' , @level1type = N'TABLE' ,@level1name = @tn; 9 FETCH NEXT FROM c INTO @tn 10 END 11 CLOSE c
12 DEALLOCATE c 13 GO 14 15 --列描述 16 DECLARE @tn NVARCHAR(50),@cn NVARCHAR(50) 17 DECLARE c CURSOR FOR SELECT t.name AS tn,l.name AS cn FROM sys.[columns] AS l INNER JOIN sys.extended_properties AS p ON l.[object_id]=p.major_id AND l.column_id=p.minor_id INNER JOIN sys.tables AS t ON l.[object_id]=t.[
object_id] WHERE t.[type]='U' AND p.[name] = N'MS_Description' ORDER BY t.name,l.name 18 OPEN c 19 FETCH NEXT FROM c INTO @tn,@cn 20 WHILE @@FETCH_STATUS=0 21 BEGIN 22 EXEC sys.sp_dropextendedproperty @name = N'MS_Description' ,@level0type = N'SCHEMA', @level0name = N'gxjt' , @level1type = N'TABLE' ,@level1name = @tn,@level2type = N'COLUMN',@level2name=@cn; 23 FETCH NEXT FROM c INTO @tn,@cn 24 END 25 CLOSE c 26 DEALLOCATE c 27 GO