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

Mike picture Mike · Dec 31, 2009 · Viewed 34.1k times · Source

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?

Answer

DOK picture DOK · Dec 31, 2009

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)