7

How do I add a column, with a default value, to an existing table in SQLServer 2008?

Mahendra Sahu
  • 179
  • 2
  • 2
  • 8

2 Answers2

20

ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

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

Pritesh Tayade
  • 630
  • 4
  • 11
9

Use ALTER TABLE. For example

ALTER TABLE Table1 
ADD  col2 INT DEFAULT 0;

SQLFiddle

peterm
  • 91,357
  • 15
  • 148
  • 157
  • 2
    As a general rule, it is best to name your constraints. If you don't it will get a random name, and they will differ between different databases (eg at different client installations). This will make scripting changes difficult in some cases (ie you will have to resort to workarounds). – TT. Jun 05 '13 at 06:22
  • 1
    @TT. Fair point. Agreed. – peterm Jun 05 '13 at 07:47