Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a default constratint on DateColumn getdate()-1I have used enterprise manager to update it to yesterday's date everyday. I would like to have a SQL which can check for the date in the systemor even a trigger which checks when the date changes the constraint is updated itself. If this is not possible I would like to have a stored procedure which I will schedule to run as a job everyday once.So if today 6/12/2006, the default value in the Datecolumn should be 6/11/2006.This gives me a error, i tried but could not fix the bug.Alter Table TABLE_NAMEAlter Constraint DF_DATECOLUMNDefault getdate()-1Ashley Rhodes
Ifor
Aged Yak Warrior
700 Posts
Posted - 2007-06-12 : 09:13:45
I do not think you can alter a CONSTRAINT. Try dropping and adding again.
ALTER TABLE TABLE_NAMEDROP CONSTRAINT DF_DateColumnGOALTER TABLE TABLE_NAMEADD CONSTRAINT DF_DateColumn DEFAULT DATEADD(day, -1 , DATEDIFF(day, 0, GETDATE())) FOR DateColumnGO
ashley.sql
Constraint Violating Yak Guru
299 Posts
Posted - 2007-06-12 : 09:13:48
I guess I have to do this everytimealter table TABLE_NAMEdrop constraint DFCONSTRAINTalter table TABLE_NAMEadd constraint DFCONSTRAINT default getdate()-1for COLUMN_NAMEAshley Rhodes
ashley.sql
Constraint Violating Yak Guru
299 Posts
Posted - 2007-06-12 : 09:15:27
IFOR you posted 3 seconds before me.Question? is my default syntax getdate()-1 not perfect.I tested and I don't get errors.Ashley Rhodes
Ifor
Aged Yak Warrior
700 Posts
Posted - 2007-06-12 : 09:19:19
My version makes the time 00:00:00. Yours has the current time a day earlier. It depends what you want.