SQL Server 2008上关于创建表时的SQL注释

39

我需要在SQL Server 2008中创建一些相当大的表格。虽然我有SQL Server Management Studio (SSMS),但我想在创建表格和列时添加注释。我该怎么做?

我要运行的查询示例:

CREATE TABLE cert_Certifications
(
  certificationID int PRIMARY KEY IDENTITY,
  profileID int,
  cprAdultExp datetime null
)

我尝试在数据类型后面添加COMMENT'成人CPR的有效期'和COMMENT ='成人CPR的有效期',但SQL Server报错了。


请参阅SQL Server通过脚本管理列描述,其中包含方便的自定义存储过程。 - Vadzim
7个回答

44

这是我使用的内容

/*==============================================================*/
/* Table: TABLE_1                                               */
/*==============================================================*/
create table TABLE_1 (
   ID                   int                  identity,
   COLUMN_1             varchar(10)          null,
   COLUMN_2             varchar(10)          null,
   constraint PK_TABLE_1 primary key nonclustered (ID)
)
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   'This is my table comment',
   'user', @CurrentUser, 'table', 'TABLE_1'
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   'This is the primary key comment',
   'user', @CurrentUser, 'table', 'TABLE_1', 'column', 'ID'
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   'This is column one comment',
   'user', @CurrentUser, 'table', 'TABLE_1', 'column', 'COLUMN_1'
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   'This is column 2 comment',
   'user', @CurrentUser, 'table', 'TABLE_1', 'column', 'COLUMN_2'
go

1
非常好的文档回答。谢谢。 - Mike Malter

32

您可以通过创建所谓的扩展属性在表格和列上添加注释。您可以在表格级别和列级别上设置扩展属性。这可以通过T-SQL或SSMS完成。

例如,在T-SQL中,它看起来像这样:

sp_addextendedproperty 'BackColor', 'Red', 'user', '<schema name>', 'table', '<table name', 'column', '<column name>'.

你可以在 sp_addextendedproperty (Transact-SQL) 中了解更多相关信息。

18

这篇文章中有好的答案。补充一点,'MS_Description' 的值也可以是其他内容。例如,我们可以使用'SourceDescription'来描述数据源的详细信息,'TableDescription'用于表格,'ColumnDescription'用于每个表格列。

示例:

-- Create example table
create table testTablename(
    id int,
    name varchar(20),
    registerNumber bigint
)

-- SourceDescription
EXEC sys.sp_addextendedproperty 
    @name=N'SourceDescription', 
    @value=N'Result of process x union y ' , -- Comment about the source this data. 
    @level0type=N'SCHEMA',
    @level0name=N'dbo', 
    @level1type=N'TABLE',
    @level1name=N'testTableName' -- Name of Table

-- TableDescription
EXEC sys.sp_addextendedproperty 
    @name=N'TableDescription', 
    @value=N'Table is used for send email to clients.' , -- Coment about the used of table
    @level0type=N'SCHEMA',
    @level0name=N'dbo', 
    @level1type=N'TABLE',
    @level1name=N'testTableName'

-- ColumnDescription
EXECUTE sp_addextendedproperty 
    @name = 'ColumnDescription', 
    @value = 'Unique identification of employer. Its the registry of company too.', 
    @level0type = 'SCHEMA', 
    @level0name= N'dbo', 
    @level1type = N'TABLE', 
    @level1name = N'testTableName', 
    @level2type = N'COLUMN', 
    @level2name = N'registerNumber'

-- If necessary, you can delete the comment.
exec sp_dropextendedproperty
    @name = 'ColumnDescription', 
    @level0type = 'SCHEMA', 
    @level0name= N'dbo', 
    @level1type = N'TABLE', 
    @level1name = N'testTableName', 
    @level2type = N'COLUMN', 
    @level2name = N'registerNumber'


-- Show you the table resume
select 
    tables.name tableName,
    tables.create_date,
    tables.modify_date,
    tableDesc.value TableDescription,
    sourceDesc.value SourceDescription
from 
    sys.tables  
    left join sys.extended_properties tableDesc on tables.object_id = tableDesc.major_id and tableDesc.name = 'TableDescription'
    left join sys.extended_properties sourceDesc on tables.object_id = sourceDesc.major_id and sourceDesc.name = 'SourceDescription'
where 
    tableDesc.name in('TableDescription', 'SourceDescription', 'ColumnDescription')
order by tables.name


-- show you the columns resume
select 
    tables.name tableName,
    columns.name columnName,
    extended_properties.value
from 
    sys.tables 
    inner join sys.columns on tables.object_id = columns.object_id
    left join sys.extended_properties on 
        tables.object_id = extended_properties.major_id 
        and columns.column_id = extended_properties.minor_id
        and extended_properties.name in('MS_Description','ColumnDescription')
where
    tables.name = 'testTableName'

4
我越看SQL Server越觉得Oracle更好!:P - Ron Jensen
使用 MS SQL 对注释或表或列进行注释相对于其他数据库来说是一项繁琐的任务。虽然在创建表之后,可以使用 GUID 更轻松地进行注释。但是,当导入表定义以及注释时会很麻烦。 - gg89

17

我喜欢在设计表格时使用图形用户界面(GUI),因为这样我可以更好地可视化布局。在GUI设计器中,可以在属性窗口中为表格和列添加描述,如下图所示:

图片描述


1
我喜欢使用GUI而不是sp_addextendedproperty(更高效)。但是如何获取表属性?除了MS开发工具Visual Studio之外,还有其他工具吗?我正在使用VS 2013专业版。 - gg89
1
这些基于GUI的描述区域已被移除,不再可用。 - Eralper

5

2
尽管它并没有直接回答原始问题(J Henzel 和 Randy Minder 已经回答了!),但我想分享一些其他的东西,我刚写的,对于那些必须频繁评论表格和列的人来说非常有用。
以下查询:
-- Generate comments templates for all tables
SELECT 
'EXEC sys.sp_addextendedproperty
    @name=N''TableDescription'',
    @level0type=N''SCHEMA'',    
    @level1type=N''TABLE'',
    @level0name=N''' + TABLE_SCHEMA + ''',
    @level1name=N''' + TABLE_NAME + ''',
    @value=N''TODO'';'
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME NOT like 'sys%'
order by TABLE_SCHEMA, TABLE_NAME


-- Generate comments templates for all columns
SELECT 'EXECUTE sp_addextendedproperty 
    @name = ''ColumnDescription'', 
    @level0type = ''SCHEMA'', 
    @level1type = N''TABLE'', 
    @level2type = N''COLUMN'', 
    @level0name=N''' + TABLE_SCHEMA + ''',
    @level1name=N''' + TABLE_NAME + ''',
    @level2name = N''' + COLUMN_NAME + ''',
    @value = ''TODO'';'
FROM   INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA not like 'sys%' and TABLE_NAME not like 'sysdiagrams%'
  order by TABLE_SCHEMA, TABLE_NAME, case when ORDINAL_POSITION = 1 then '0' else COLUMN_NAME end

在SQL Server中,通过查询系统表来收集所有现有表和列的sp_addextendedproperty调用列表,将产生一个列表。 当然,它不会为您自动编写注释,但是您只需使用相关注释填写“TODOs”占位符即可描述所有对象并执行它。 它避免了手动编写所有调用并节省了大量时间,而且您不会忘记表或列,因此我希望它对其他人有用。 注意:“sys”中的WHERE过滤器排除了系统对象,但是根据您的对象名称,您可能需要进行一些微调以使其更精细。 此外,我的数据库中没有任何注释,因此我的查询返回所有表/列。 它不考虑是否已经有注释。

我对获取一个特定表的模板进行了微小的更改 SELECT 'EXECUTE sp_addextendedproperty @name = ''ColumnDescription'',@level0type = ''SCHEMA'', '@level1type= N''TABLE'', @level2type= N''COLUMN'', @level0name=N''' + TABLE_SCHEMA + ''', @level1name=N''subacct'', @level2name= N''' + COLUMN_NAME + ''', @value` = ''TODO'';' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA not like 'sys%' and TABLE_NAME ='subacct' order by TABLE_SCHEMA, TABLE_NAME, case when ORDINAL_POSITION = 1 then '0' else COLUMN_NAME end - gg89

-5
使用以下SQL命令: 创建表 TABLE NAME (ATTRIBUTE NAME (ATTRIBUTE SIZE)) // createtable都是关键字

这与注释有什么关系? - Peter Mortensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接