1

I'm trying to add a default constraint to an existing field in SQL Server using the following T-SQL:

alter table Extra add constraint DF_Extra_ExternalRef DEFAULT ('*') for ExternalRef with values

This adds the default constraint but fails to update existing records with null values.

I'm using SQL Server 2005.

asked Oct 4, 2013 at 14:52

1 Answer 1

5

I've only ever seen WITH VALUES used this way when adding a new column (and this is all that is documented). If you're adding a constraint to an existing column, I think WITH VALUES is a no-op. Therefore:

ALTER TABLE dbo.Extra ADD CONSTRAINT DF_Extra_ExternalRef 
 DEFAULT ('*') FOR ExternalRef;
UPDATE dbo.Extra SET ExternalRef = '*' WHERE ExternalRef IS NULL;
answered Oct 4, 2013 at 14:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.