Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

Add a column, with a default value, to an existing table in SQL Server

$
0
0


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}
[WITHVALUES]
Note: Use WITH VALUES to update existing null-able rows.

Example:
ALTERTABLE MyTable
ADD MyTableTypeID INTNOT NULL
CONSTRAINT Constraint_MyTableTypeID DEFAULT0
GO

Note: WITH VALUES handles the NOT NULL part

ALTERTABLE MyTable
ADD MyTableTypeID INT
CONSTRAINT Constraint_MyTableTypeID DEFAULT 0 WITH VALUE
GO

Viewing all articles
Browse latest Browse all 265

Trending Articles