SQLServer 添加、查看、删除、更新注释
SQLServer 添加、查看、删除、更新注释
用例表
CREATE TABLE dbo.book
(
bookId BIGINT NOT NULL PRIMARY KEY,
bookname NVARCHAR(50) NOT NULL, --书名
author NVARCHAR(50) NOT NULL--作者
);
一、表注释
1、添加注释
---原型
EXEC sys.sp_addextendedproperty
@name='MS_Description',@value='注释内容',
@level0type='SCHEMA',@level0name='dbo',
@level1type='TABLE',@level1name='book';
--简约版
EXEC sys.sp_addextendedproperty
'MS_Description','注释内容',
'SCHEMA','dbo',
'TABLE','book';
2、修改注释
EXEC sys.sp_updateextendedproperty
'MS_Description','更新的注释内容',
'SCHEMA','dbo',
'TABLE','book';
3、删除注释
EXEC sys.sp_dropextendedproperty
'MS_Description',
'SCHEMA','dbo',
'TABLE','book';
4、查看注释
SELECT st.name tablename, sep.value tabledesc
FROM sys.tables st
JOIN sys.extended_properties sep
ON sep.major_id = st.object_id
AND minor_id = 0
三、字段注释
1、添加注释
---原型
EXEC sys.sp_addextendedproperty
@name=N'MS_Description',@value=N'注释内容',
@level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'book'
@level2type='COLUMN',@level2name='AUTHOR';
--简约版
EXEC sys.sp_addextendedproperty
'MS_Description','注释内容' ,
'user','dbo',
'table','book',
'column','bookname';
2、修改注释
EXEC sys.sp_updateextendedproperty
'MS_Description','更新的注释内容',
'SCHEMA','dbo',
'TABLE','book';
'column','bookname';
3、删除注释
EXEC sys.sp_updateextendedproperty
'MS_Description','更新的注释内容',
'SCHEMA','dbo',
'TABLE','book';
'column','bookname';
4、查看注释
SELECT st.name AS '表名', sc.name AS '字段名', sep.value AS '字段注释'
FROM sys.tables st
JOIN sys.columns sc ON sc.object_id = st.object_id
LEFT JOIN sys.extended_properties sep
ON sep.major_id = sc.object_id AND sep.minor_id = sc.column_id
WHERE st.name = 'book'