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

早过忘川 提交于 2019-11-30 12:01:22

问题


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.

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?


回答1:


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 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)



回答2:


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.




回答3:


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 picture




回答4:


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

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



来源:https://stackoverflow.com/questions/1985254/sql-server-can-you-add-field-descriptions-in-create-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!