Sql Server 清除表、字段注释信息

发布时间:2023年12月26日

防止他人拷贝数据库还能看到注释信息

-- 清除指定库所有表的注释
DECLARE @DatabaseName NVARCHAR(100) = 'YourDatabaseName'; -- 替换为实际的数据库名

DECLARE @TableName NVARCHAR(100);

DECLARE tableCursor CURSOR FOR
    SELECT TABLE_NAME AS TableName
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME IN (SELECT t.name
                         FROM sys.tables t
                                  LEFT JOIN sys.extended_properties ep
                                            ON ep.major_id = t.object_id AND ep.minor_id = 0 AND
                                               ep.name = 'MS_Description'
                         WHERE ep.value IS NOT NULL);

OPEN tableCursor;

FETCH NEXT FROM tableCursor INTO @TableName;

WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE @Sql NVARCHAR(MAX);

        SET @Sql = 'EXEC sp_dropextendedproperty ''MS_Description'', ''SCHEMA'', ''dbo'', ''TABLE'', ''' + @TableName +
                   ''', NULL, NULL;';

        EXEC sp_executesql @Sql;

        FETCH NEXT FROM tableCursor INTO @TableName;
    END

CLOSE tableCursor;
DEALLOCATE tableCursor;

-- 清除指定库所有表中的字段注释
DECLARE @DatabaseName NVARCHAR(100) = 'YourDatabaseName'; -- 替换为实际的数据库名

DECLARE @TableName NVARCHAR(100);
DECLARE @ColumnName NVARCHAR(100);

DECLARE columnCursor CURSOR FOR
    SELECT TABLE_NAME AS TableName, COLUMN_NAME AS ColumnName
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME + '.' + COLUMN_NAME IN (SELECT DISTINCT OBJECT_NAME(major_id) + '.' + COL_NAME(major_id, minor_id)
                                             FROM sys.extended_properties
                                             WHERE class_desc = 'OBJECT_OR_COLUMN'
                                               AND major_id > 0
                                               AND minor_id > 0
                                               AND name = 'MS_Description');

OPEN columnCursor;

FETCH NEXT FROM columnCursor INTO @TableName, @ColumnName;

WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE @Sql NVARCHAR(MAX);

        SET @Sql = 'EXEC sp_dropextendedproperty ''MS_Description'', ''SCHEMA'', ''dbo'', ''TABLE'', ''' + @TableName +
                   ''', ''COLUMN'', ''' + @ColumnName + ''';';

        EXEC sp_executesql @Sql;

        FETCH NEXT FROM columnCursor INTO @TableName, @ColumnName;
    END

CLOSE columnCursor;
DEALLOCATE columnCursor;

-- 查询当前库所有表是否存在注释
SELECT t.name   AS TableName,
       ep.value AS TableComment
FROM sys.tables t
         LEFT JOIN sys.extended_properties ep
                   ON ep.major_id = t.object_id AND ep.minor_id = 0 AND ep.name = 'MS_Description'
WHERE ep.value IS NOT NULL

?

文章来源:https://blog.csdn.net/ip36900044/article/details/135229139
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。