Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default getdate for Insert date

I have a table called sample and it has a column called [__INSERT_DATE] which is null. Now I want to alter the column with default as getdate(). When I tried the following it gave me an error.

ALTER TABLE sample
ALTER COLUMN [__INSERT_DATE] [datetime] DEFAULT (getdate()) NULL)

Can anyone tell me what the problem is?


2 Answers

Try this:

ALTER TABLE MyTable ADD CONSTRAINT
DF_MyTable_Inserted DEFAULT GETDATE() FOR INSERT_DATE
GO

This assumes your table is named MyTable, the column is INSERT_DATE, and the name of the contstraint is to be DF_MyTable_Inserted

like image 82
p.campbell Avatar answered Sep 09 '25 15:09

p.campbell


Try this:

ALTER TABLE sample ADD CONSTRAINT DF_sample___INSERT_DATE DEFAULT(GETDATE()) FOR __INSERT_DATE
like image 31
J.Ray Avatar answered Sep 09 '25 16:09

J.Ray