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

给表和列增加注释,通过增加扩展属性来实现


代码如下:

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'


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值