SQLServer表、列備註管理

在開發時,爲了方便,在SQL SERVER庫內對錶名、列名進行備註,但在DB部署客戶時,想一次清理上述備註值。spa

特編寫以下 SQL,一次清除上述備註內容:code

 
declare @table nvarchar(500)
declare @colName nvarchar(500)
declare @description nvarchar(500)
declare @descType nvarchar(500)
declare @name nvarchar(500)
declare @level0typeV nvarchar(500)
declare @level0nameV nvarchar(500)
declare @level1typeV nvarchar(500)
declare @level1nameV nvarchar(500)
declare @level2typeV nvarchar(500)
declare @level2nameV nvarchar(500)

select  @name= 'MS_Description',  
        @level0typeV = 'SCHEMA',
        @level0nameV = 'dbo',
        @level1typeV = 'TABLE',
        @level2typeV = 'COLUMN'
 
 
declare cur_desc cursor for
   SELECT
        convert(varchar(500), A.name) AS table_name,
        convert(varchar(500),B.name) AS column_name,
        ISNULL(convert(varchar(500), C.value),'') AS Description,
        'Column' as DescType
    FROM sys.tables A
       Inner JOIN sys.columns B ON B.object_id = A.object_id
       LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id
   union All
   SELECT
        convert(varchar(500), A.name) AS table_name,
        null AS column_name,
        ISNULL(convert(varchar(500), C.value),'') AS Description,
        'Table' as DescType
    FROM sys.tables A     
       LEFT JOIN sys.extended_properties C ON C.major_id = A.object_id
open cur_desc
FETCH NEXT FROM cur_desc INTO @table, @colName, @description, @colName
WHILE @@FETCH_STATUS = 0
BEGIN
    if @descType = 'Column'    
         EXEC sp_dropextendedproperty @name = 'MS_Description', @Level0type = @level0typeV, @Level0name = @level0nameV, 
              @Level1type = @level1typeV, @Level1name = @table, @Level2type = @level2typeV, @Level2name = @colName 
    else
         EXEC sp_dropextendedproperty @name = 'MS_Description', @Level0type = @level0typeV, @Level0name = @level0nameV, 
              @Level1type = @level1typeV, @Level1name = @table 
    FETCH NEXT FROM cur_desc INTO @table, @colName, @description, @descType
END 
CLOSE cur_desc;
DEALLOCATE cur_desc;
 
 
相關文章
相關標籤/搜索