How can a column with a default value be added to an existing table?

Technology CommunityCategory: SQLHow can a column with a default value be added to an existing table?
VietMX Staff asked 3 years ago

Consider:

ALTER TABLE SomeTable
        ADD SomeCol Bit NULL --Or NOT NULL.
 CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is autogenerated.
    DEFAULT (0)--Optional Default-Constraint.
WITH VALUES --Add if Column is Nullable and you want the Default Value for Existing Records.

Keep in mind that if the column is nullable, then null will be the value used for existing rows.