SQL 添加列描述

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3754180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:36:15  来源:igfitidea点击:

adding a column description

sqlsql-serversql-server-2005

提问by EJC

Does anyone know how to add a description to a SQL Server column by running a script? I know you can add a description when you create the column using SQL Server Management Studio.

有谁知道如何通过运行脚本向 SQL Server 列添加描述?我知道您可以在使用 SQL Server Management Studio 创建列时添加说明。

How can I script this so when my SQL scripts create the column, a description for the column is also added?

我该如何编写脚本,以便在我的 SQL 脚本创建列时,还会添加该列的描述?

回答by Abe Miessler

I'd say you will probably want to do it using the sp_addextendedpropertystored proc.

我会说你可能想要使用sp_addextendedproperty存储过程来完成它。

Microsoft has some good documentation on it.

微软有一些很好的文档。

Try this:

尝试这个:

EXEC sp_addextendedproperty 
    @name = N'MS_Description', @value = 'Hey, here is my description!',
    @level0type = N'Schema',   @level0name = 'yourschema',
    @level1type = N'Table',    @level1name = 'YourTable',
    @level2type = N'Column',   @level2name = 'yourColumn';
GO

回答by JosephStyons

This works for me. Relevant arguments are indicated with little arrows.

这对我有用。相关论点用小箭头表示。

EXEC sys.sp_addextendedproperty 
  @name=N'MS_Description'
 ,@value=N'Here is my description!'  --<<<<
 ,@level0type=N'SCHEMA'
 ,@level0name=N'dbo'
 ,@level1type=N'TABLE'
 ,@level1name=N'TABLE_NAME' --<<<<
 ,@level2type=N'COLUMN'
 ,@level2name=N'FIELD_NAME'  --<<<<

回答by DForck42

EXEC sys.sp_addextendedproperty @name = N'MS_Description', 
@value = N'extended description', 
@level0type = N'SCHEMA',
@level0name = N'dbo',
@level1type = N'TABLE',
@level1name = N'Table_1',
@level2type = N'COLUMN',
@level2name = N'asdf'

Create script on table [dbo].[Table_1]

在表 [dbo].[Table_1] 上创建脚本

回答by Ben Slade

In MS SQL Server Management Studio 10.0.55, the easiest way is to:

在 MS SQL Server Management Studio 10.0.55 中,最简单的方法是:

  • Display the columns for the table in the Object Explorer window
  • Right click on the column of interest and click on the "Modify" option
  • Look in the "Column Properties" window (in the lower right in my GUI)\
  • Look in the "Table Designer" sub section
  • Modify the value for the "Description" row
  • Click on the "x" in the upper right of the column modification window/tab
  • Answer "y" when it says apply changes
  • 在对象资源管理器窗口中显示表的列
  • 右键单击感兴趣的列,然后单击“修改”选项
  • 查看“列属性”窗口(在我的 GUI 的右下角)\
  • 查看“表设计器”子部分
  • 修改“描述”行的值
  • 单击列修改窗口/选项卡右上角的“x”
  • 当它说应用更改时回答“y”

If you then right click on your table in the Object Explorer window and click on properties, then click on "Extended Properties", you should see your comment.

如果您随后在对象资源管理器窗口中右键单击您的表并单击属性,然后单击“扩展属性”,您应该会看到您的注释。

Note, if you do a "Script Table As" command for the table, the above column "Description" still doesn't show up as a comment for the column. Instead it shows an extra sp_addextendedproperty call after the table create. Mediocre.

请注意,如果您对该表执行“Script Table As”命令,上面的“Description”列仍不会显示为该列的注释。相反,它在表创建后显示一个额外的 sp_addextendedproperty 调用。平庸。