How to added to an existing table on SQL server 2005 or above

You can do it by edit of table design, if you want do it with query see following.

Syntax:

ALTER TABLE {TABLENAME}

ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}

CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

[WITH VALUES]

Note: Use WITH VALUES to update existing null-able rows.

Example:

ALTER TABLE MyTable

ADD MyTableTypeID INT NOT NULL

CONSTRAINT Constraint_MyTableTypeID DEFAULT 0

GO

Note: WITH VALUES handles the NOT NULL part

ALTER TABLE MyTable

ADD MyTableTypeID INT

CONSTRAINT Constraint_MyTableTypeID DEFAULT 0 WITH VALUE

GO

 

try this for adding existing table

[ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
[WITH VALUES]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s