SQL Server 表注释和字段注释

1. 查询注释

查询表的所有注释

SELECT A.name ,C.value
FROM sys.tables A
    LEFT JOIN sys.extended_properties C ON C.major_id = A.object_id
WHERE C.minor_id=0
group by A.name ,C.value

查询表的所有字段注释

SELECT A.name AS table_name,B.name AS column_name,C.value AS column_description
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
WHERE A.name = 't_bd_hobby' -- 表名

2. 添加表注释

-- 表加注释
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'注释内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'表名'
--例如:
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'系统设置表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CM01_SYSTEM'

3.添加字段注释

 -- 字段加注释
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'注释内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'表名', @level2type=N'COLUMN',@level2name=N'字段名'
posted @ 2022-09-06 20:18  陆陆无为而治者  阅读(428)  评论(0编辑  收藏  举报