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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inserting sysdate in a new column

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2012-05-09 : 13:14:59
Hi All
There is a need to add a new column(SystemDate) in an Existing Table(customerInfo).The new column should automatically get populated with sysdate at the time of a record gets inserted

What would be the Alter script for the new column.I used the below script and it is not correct

ALTER TABLE CustomerInfo
ADD SystemDate as (getdate ( ))


Thanks

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-09 : 13:22:59
Maybe this weill help get you going
ALTER TABLE dbo.CustomerInfo
ADD SystemDate DATETIME2(2) CONSTRAINT DF_CustomerInfo_SystemDate DEFAULT (getdate()) NOT NULL
Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2012-05-09 : 14:47:33
Thank You.It worked
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-09 : 15:54:41
quote:
Originally posted by jim_jim

Thank You.It worked


please keep in mind that defaults wont get applied for updates so you've explicitly pass the value for this column in update statements to get it changed

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -