wsturdev
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
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
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
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
ALTER TABLE MyTable ALTER COLUMN MyColumn SET DEFAULT 0;