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

  • If you add "WITH VALUES" to your ALTER TABLE ADD statement, it will put the default value in regardless of the NULL/NOT NULL setting...

    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 NULL DEFAULT '1900-01-01' WITH VALUES;

    GO

    SELECT

    *

    FROM

    dbo.test AS T;

    GO

    DROP TABLE dbo.test;