Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Comments on Create Table on SQL Server 2008

I need to create some pretty big tables in SQL Server 2008, while I do have SQL Server Management Studio, I would like to comment the tables and the columns when I create the table. How do I do this?

Example of the query I am running:

CREATE TABLE cert_Certifications (   certificationID int PRIMARY KEY IDENTITY,   profileID int,   cprAdultExp datetime null ) 

I've tried COMMENT'Expiration Date for the Adult CPR' and COMMENT='Expiration Date for the Adult CPR' after the data type, and SQL Server is giving me an error.

like image 555
Snow_Mac Avatar asked Jan 03 '11 17:01

Snow_Mac


People also ask

How do I create a table in SQL Server Management Studio 2008?

In SSMS, in Object Explorer, connect to the instance of Database Engine that contains the database to be modified. In Object Explorer, expand the Databases node and then expand the database that will contain the new table. In Object Explorer, right-click the Tables node of your database and then click New Table.

How can I get table comments in SQL Server?

Right click the table or column and select properties. Look in the extended properties. If the database has a diagram, check the description field in the properties of the table or column. You can also query that data from the "ms_description" extended property.

How do you add a comment to a table in SQL?

To add a comment to a table, view, or materialized view, you must have COMMENT ANY TABLE system privilege. To add a comment to an indextype, you must have the CREATE ANY INDEXTYPE system privilege. To add a comment to an operator, you must have the CREATE ANY OPERATOR system privilege.


1 Answers

This is what I use

/*==============================================================*/ /* Table: TABLE_1                                               */ /*==============================================================*/ create table TABLE_1 (    ID                   int                  identity,    COLUMN_1             varchar(10)          null,    COLUMN_2             varchar(10)          null,    constraint PK_TABLE_1 primary key nonclustered (ID) ) go  declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty 'MS_Description',     'This is my table comment',    'user', @CurrentUser, 'table', 'TABLE_1' go  declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty 'MS_Description',     'This is the primary key comment',    'user', @CurrentUser, 'table', 'TABLE_1', 'column', 'ID' go  declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty 'MS_Description',     'This is column one comment',    'user', @CurrentUser, 'table', 'TABLE_1', 'column', 'COLUMN_1' go  declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty 'MS_Description',     'This is column 2 comment',    'user', @CurrentUser, 'table', 'TABLE_1', 'column', 'COLUMN_2' go 
like image 134
J Henzel Avatar answered Sep 16 '22 14:09

J Henzel