Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading