SQL Server - 你能在 CREATE TABLE 中添加字段描述吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1985254/
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 04:54:14  来源:igfitidea点击:

SQL Server - Can you add field descriptions in CREATE TABLE?

sqlsql-servercreate-tablefield-description

提问by Mike

I can see plenty of posts about where the field description extended property lives and how I can get it, but nothing about adding these at the CREATE TABLE stage.

我可以看到很多关于字段描述扩展属性的位置以及如何获取它的帖子,但没有关于在 CREATE TABLE 阶段添加这些内容的帖子。

I'm dynamically creating tables so dynamically adding field descriptions would be a tidy thing to do but I cannot see a way.

我正在动态创建表,因此动态添加字段描述将是一件整洁的事情,但我看不到方法。

Has anyone managed to do this?

有没有人设法做到这一点?

回答by DOK

While you can't do it in CREATE TABLE, you can do it at the same time, in the same database script, using this approach:

虽然你不能做到这一点的CREATE TABLE,你可以在同一时间做,在同一个数据库的脚本,使用这种方法

CREATE table T1 (id int , name char (20))

EXEC   sp_addextendedproperty 'MS_Description', 'Employee ID', 'user', dbo, 'table', 'T1', 'column', id

EXEC   sp_addextendedproperty 'MS_Description', 'Employee Name', 'user', dbo, 'table', 'T1', 'column', name

Then you can see your entries using this:

然后您可以使用以下命令查看您的条目:

SELECT   *
FROM   ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', 'T1', 'column', default)

回答by Randy Minder

I don't believe the Create Table T-SQL statement supports this. However, if you are defining your tables via SSMS, you can easily enter table level and column level comments at the same time you create your table.

我不相信 Create Table T-SQL 语句支持这一点。但是,如果您通过 SSMS 定义表,则可以在创建表的同时轻松输入表级和列级注释。

回答by Muhammad

In addition to the above, you can also use SSMS to do it. In SSMS, Right click on the table, select Properties, then click "Extended Properties" (on the left pane). On the right pane, in the middle, there is a "Properties" box, click in there to give your description a name, and some text, as shown in the attached pictureScreen Capture for Adding Descriptive Text to a Table Using SSMS

除了上面的,你还可以使用SSMS来做。在 SSMS 中,右键单击表格,选择“属性”,然后单击“扩展属性”(在左侧窗格中)。在右侧窗格的中间,有一个“属性”框,点击那里为您的描述命名,以及一些文本,如附图所示使用 SSMS 向表格添加描述性文本的屏幕截图

回答by Muhammad

SQL Server provides a system stored procedure that allows you to add descriptions, one name-value pair at a time

SQL Server 提供了一个系统存储过程,允许您一次添加一个名称-值对

Example as follows:

示例如下:

EXEC sys.sp_addextendedproperty 
@name=N'Column 2 Description' -- Give your description a name
   , @value=N'blah blah 2 for a specific column' -- Actual description
   , @level0type=N'SCHEMA'
   , @level0name=N'dbo'
   , @level1type=N'TABLE'
   , @level1name=N'MyTestTable' -- Name of your table
GO

You would have to repeat for each description name-value pair

您必须为每个描述名称-值对重复

Hope this helps

希望这可以帮助