Home Forums SQL Server 2012 SQL 2012 - General Adding columns to existing table RE: Adding columns to existing table<!-- 864 -->

  • If you create the new columns as NOT NULL with a DEFAULT constraint then the columns will be populated with whatever you put in for the DEFAULT. Like this:

    USE tempdb;

    GO

    CREATE TABLE test (c INT NOT NULL);

    GO

    INSERT INTO dbo.test

    (c)

    VALUES

    (0 -- c - int

    );

    GO

    ALTER TABLE dbo.test

    ADD create_date SMALLDATETIME NOT NULL DEFAULT '1900-01-01';

    GO

    SELECT

    *

    FROM

    dbo.test AS T;

    GO

    DROP TABLE dbo.test;