- Default values are treated like constraints
- Therefore, changing a default value means dropping and recreating the
constraint with the new value.
- If the default value was declared inline (ie, without a constraint name),
then you need to look up the constraint name somehow.
- See also sys-default-constraints docs
DECLARE @constraint_name VARCHAR(100) = (
SELECT dc."name" FROM sys.default_constraints dc
WHERE dc.parent_object_id = object_id('SomeTable')
AND dc.parent_column_id = (SELECT column_id FROM sys.columns WHERE name = 'some_column' AND object_id = object_id('SomeTable'))
)
EXEC('ALTER TABLE SomeTable DROP CONSTRAINT ' + @constraint_name + ';');
ALTER TABLE SomeTable
ADD CONSTRAINT DF_some_column_default_true DEFAULT (1) FOR some_column