views:

219

answers:

2

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  A: 

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.

Randy Minder
Hi, Thanks for the response, I know I'm clutching at straws here, I can do as you say via SSMS but I'm programmatically creating a table on the fly. I build up my CREATE TABLE command over a number of other routines and I was just wondering if I could jab in a usefull comment or two on the way.I'm kind of hoping that someone found a way or work around to do this. Thanks
Mike
If you are programmatically creating a table on the fly, why not also programmatically add the comment extended property as well?
Randy Minder
+3  A: 

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)
DOK
Awsome does exactly what I want. Many thanks.
Mike