I have a table and one of the columns is "Date" of type datetime. We decided to add a default constraint to that column
Alter table TableName
alter column dbo.TableName.Date default getutcdate()
but this gives me error:
Incorrect syntax near '.'
Does anyone see anything obviously wrong here, which I am missing (other than having a better name for the column)
Try this
example
also make sure you name the default constraint..it will be a pain in the neck to drop it later because it will have one of those crazy system generated names...see also How To Name Default Constraints And How To Drop Default Constraint Without A Name In SQL Server
Actually you have to Do Like below Example, which will help to Solve the Issue...
You're specifying the table name twice. The ALTER TABLE part names the table. Try: Alter table TableName alter column [Date] default getutcdate()
you can wrap reserved words in square brackets to avoid these kinds of errors:
I use the stored procedure below to update the defaults on a column.
It automatically removes any prior defaults on the column, before adding the new default.
Examples of usage:
Stored procedure:
Errors this stored procedure eliminates
If you attempt to add a default to a column when one already exists, you will get the following error (something you will never see if using this stored proc):