SQL Server 2008上创建表的SQL注释

Sno*_*Mac 27 sql sql-server-2008

我需要在SQL Server 2008中创建一些非常大的表,而我确实有SQL Server Management Studio,我想在创建表时对表和列进行注释.我该怎么做呢?

我正在运行的查询示例:

CREATE TABLE cert_Certifications
(
  certificationID int PRIMARY KEY IDENTITY,
  profileID int,
  cprAdultExp datetime null
)
Run Code Online (Sandbox Code Playgroud)

我在数据类型之后尝试了成人CPR的COMMENT'Expiration日期'和COMMENT ='成人CPR的截止日期',并且SQL Server给了我一个错误.

J H*_*zel 37

这就是我使用的

/*==============================================================*/
/* 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
Run Code Online (Sandbox Code Playgroud)


Ran*_*der 25

您可以通过创建所谓的扩展属性来对表和列进行注释.您可以在表级和列级放置扩展属性.这可以通过T-SQL或SSMS完成.

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

sp_addextendedproperty 'BackColor', 'Red', 'user', '<schema name>', 'table', '<table name', 'column', '<column name>'.
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读更多相关信息


RC_*_*and 14

在设计表格时我更喜欢GUI,因为我可以更好地可视化布局.在GUI设计器中,可以在属性窗口中为表和列添加描述,如下图所示 替代文字


小智 10

这篇文章有很好的答案.添加值'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'
Run Code Online (Sandbox Code Playgroud)

  • 我越看看SQL Server就越好看Oracle!:P (2认同)

Kla*_*sen 5

您需要使用名为sp_addextendedproperty的存储过程向 SQL\xc2\xa0Server 中的列/表添加注释。

\n