Link to home
Start Free TrialLog in
Avatar of wsturdev
wsturdevFlag for United States of America

asked on

Programmatically add default to column

I have a table MyTable with a column MyColumn, which is defined as BIT and currently has no default value specified.

What SQL Syntax do I use to add a Default of 0 to that column?

I have tried ALTER TABLE MyTable ALTER COLUMN MyColumn DEFAULT 0;  which does not work.
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

This should help you out:

ALTER TABLE MyTable ALTER COLUMN MyColumn SET DEFAULT 0;
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that didn't work then try creating a Default constraint as given below which would do the equivalent operation:

ALTER TABLE MyTable ADD CONSTRAINT def_constraint DEFAULT 0 FOR MyColumn;
hi wsturdev,

                Check this one.


https://www.experts-exchange.com/questions/20726441/syntax-for-adding-default-value-to-column.html
ALTER TABLE MyTable ADD CONSTRANT  DF_ConstrantName DEFAULT 0 FOR MyColumn

Open in new window

Sometimes you need to delete the prior default constraint that is automatically created by SQL Server...see this article:  http://bypsoft.blogspot.com/2007/10/changing-default-column-values-sql.html