且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向具有值的现有字段添加默认约束

更新时间:2023-02-08 20:15:51

我只见过 WITH VALUES 在添加新列时使用这种方式(这是 所有记录).如果您要向现有列添加约束,我认为 WITH VALUES 是无操作的.因此:

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;