views:

302

answers:

2

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 Incorrect syntax near '.'.

Does anyone see anything obviously wrong here, which I am missing (other than having a better name for the column)

Server: SQL Server 2008

A: 

you can wrap reserved words in square brackets to avoid these kinds of errors:

dbo.TableName.[Date]

Ray
+4  A: 

Try this

alter table TableName 
 add constraint df_ConstraintNAme 
 default getutcdate() for [Date]

example

create table bla (id int)

alter table bla add constraint dt_bla default 1 for id



insert bla default values

select * from bla

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

SQLMenace
great- worked :)
ram