您的位置:首页 > 数据库

【SQL Server学习笔记】表和列增加注释

2016-01-14 16:34 435 查看
给表和列增加注释,通过增加扩展属性来实现

代码如下:

create table ttt
(id int not null primary key,
v  varchar(100) )

--给表添加注释
--注意后面的各层类型和名称,指出了要给什么增加扩展属性
exec sp_addextendedproperty
@name = 'ttt_desc1',   --扩展属性的名称
@value = '表中的主键',  --给表添加的注释

@level0type ='schema', --第0层类型是架构
@level0name = 'dbo',   --架构名称

@level1type = 'table', --第1层类型是表
@level1name = 'ttt'

--给列添加注释
exec sp_addextendedproperty
@name = 'ttt_desc2',   --扩展属性的名称
@value = '表中的主键',  --给列添加的注释

@level0type ='schema', --第0层类型是架构
@level0name = 'dbo',   --架构名称

@level1type = 'table', --第1层类型是表
@level1name = 'ttt',   --表名称

@level2type = 'column',--第2层是列
@level2name = 'id'     --列名称

--更新列的注释
exec sp_updateextendedproperty
@name = 'ttt_desc2',                   --扩展属性的名称
@value = '表中的主键,唯一标示一行数据',  --更新列添加的注释

@level0type ='schema', --第0层类型是架构
@level0name = 'dbo',   --架构名称

@level1type = 'table', --第1层类型是表
@level1name = 'ttt',   --表名称

@level2type = 'column',--第2层是列
@level2name = 'id'     --列名称

--删除列的注释
EXEC SP_DROPextendedproperty
@name ='ttt_desc2',

@level0type ='schema', --第0层类型是架构
@level0name = 'dbo',   --架构名称

@level1type = 'table', --第1层类型是表
@level1name = 'ttt',   --表名称

@level2type = 'column',--第2层是列
@level2name = 'id'     --列名称


可以在试图中查询这些扩展信息:

--SQL Server 2000
select *
from sysproperties

--SQL Server 2005
select *
from sys.extended_properties

进一步扩展,只查询表的属性信息:

select t.object_id,t.name,p.value
from sys.tables t
inner join sys.extended_properties p
on t.object_id = p.major_id
where minor_id = 0
and t.type= 'U'

表的注释以及列的注释:

select t.object_id,
case when isnull(p.minor_id,0)=0 then '表的注释' else '列的注释' end as comment,
case when isnull(p.minor_id,0)=0 then c.name else t.name end as table_or_column,
p.value
from sys.tables t
inner join sys.extended_properties p
on t.object_id = p.major_id
left join sys.columns c
on c.column_id = p.minor_id and
c.object_id = t.object_id
where  t.type= 'U'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: