Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 705
Mike Avatar asked Dec 31 '09 12:12

Mike


People also ask

How do you create a description table?

CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Then in brackets comes the list defining each column in the table and what sort of data type it is.


2 Answers

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) 
like image 76
DOK Avatar answered Sep 20 '22 16:09

DOK


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.

like image 40
Randy Minder Avatar answered Sep 20 '22 16:09

Randy Minder