SQL Server——建表时为字段添加注释

在 MySQL 中,新建数据库表为字段添加注释可以使用 comment 属性来实现。SQL Server 没有 comment 属性,但是可以通过执行 sys.sp_addextendedproperty 这个存储过程添加扩展属性来实现相同的功能。

这个存储过程的参数定义如下:

sql 复制代码
exec sys.sp_addextendedproperty
    @name, @value,
    @level0type, @level0name,
    @level1type, @level1name,
    @level2type, @level2name
go

固定的参数如下:

sql 复制代码
exec sys.sp_addextendedproperty
    'MS_Description', @value,
    'SCHEMA', @level0name,
    'TABLE', @level1name,
    'COLUMN', @level2name
go
  • @value:要备注的内容。
  • @level0name:建表时指定的架构名。如果没有指定架构,填:dbo。
  • @level1name:表名。
  • @level2name:列名。

示例:

sql 复制代码
create table student
(
    id   bigint       not null primary key,
    name nvarchar(255),
)

exec sp_addextendedproperty 
	'MS_Description', N'名称', 
	'SCHEMA', 'dbo', 
	'TABLE', 'student', 
	'COLUMN', 'name'
go

select *
from student

DataGrip 显示效果为:

相关推荐
woshilys1 小时前
sql server 比较字符是否相同
sqlserver
赖龙2 小时前
SQL Server 日志收缩实战:从空间告急到稳定运行
sqlserver
_1_73 天前
SQL SERVER闪退问题解决
数据库·sqlserver
李白客3 天前
SQL Server 迁移注意事项:一次的真实复盘与经验沉淀
数据库·sqlserver·迁移学习
全栈小53 天前
【数据库】Sql Server,A表的a字段更新到B表的a字段,基础知识点,一分钟拿下
sqlserver
逍遥德3 天前
PostgreSQL --- 自增主键【序列】的避坑指南
数据库·后端·sql·mysql·postgresql·sqlserver
墨燚7 天前
SQL Server学习之旅
sqlserver
tongyiixiaohuang8 天前
跨平台数据库集成:SQLServer到MySQL的最佳实践
数据库·mysql·sqlserver
修电脑的猫10 天前
SAP<->SQL server链接
sqlserver·sap·abap
满昕欢喜10 天前
SQL Server 2019的常用工具
数据库·sqlserver