SQL Server not allowing datetime column to be added that has a computed default value

Advertisements

SQL Server here. Trying to add a DATETIME column to an existing table, that has a default value of "now minus 12 months" (12 months prior to whatever point in time an insert is made to the table).

My best attempt:

ALTER TABLE myschema.mytable
    ADD COLUMN Start_Date datetime NOT NULL DEFAULT DATEADD(month, -12, GETDATE()) AFTER Client;

Produces:

SQL Error [156] [S0001]: Incorrect syntax near the keyword 'COLUMN'.

Can anyone spot where I’m going awry?

>Solution :

SQL Server Syntax for add column does not include keyword COLUMN, it has to be

ALTER TABLE mytable
ADD Start_Date datetime NOT NULL DEFAULT DATEADD(month, -12, GETDATE()) 

Leave a Reply Cancel reply