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 显示效果为:

相关推荐
xuefuhe2 天前
SQL Server查看系统存储过程及视图的定义
sqlserver
techdashen2 天前
Uber 如何完成超大规模 JUnit 迁移:从 JUnit 4 到 JUnit 5 的自动化工程实践
junit·sqlserver·自动化
xuefuhe4 天前
SQL Server Configuration Manager报Cannot connect to WMI provider错误
sqlserver
殳翰6 天前
spring对junit的支持
spring·junit·sqlserver
可触的未来,发芽的智生6 天前
发现-认知系统记忆架构工程启示录
数据库·人工智能·python·程序人生·sqlserver
ClouGence7 天前
SQL Server CDC 如何降低主库压力?Always On 备库读取实践
数据库·后端·sql·sqlserver
数据库小学妹25 天前
SQL Server数据库同步工具怎么选?6款方案对比+信创迁移避坑清单
数据库·经验分享·sqlserver·dba
铁打的阿秀25 天前
SQL server2025 Express安装及管理工具安装使用教程(Windows)
windows·sqlserver·express
文盲老顾1 个月前
sqlserver 根据IP和数量,计算应该使用的掩码IP地址段
sqlserver·递归·自定义函数·cte·ip掩码·表值函数
czhc11400756631 个月前
6.11:halcon,Sqlserver;项目sql连接;git
git·sql·sqlserver