Link to home
Start Free TrialLog in
Avatar of DB-aha
DB-aha

asked on

Adding Column that Populates Timestamp upon row insertion SQL Server 2012

Hello all,

I have several tables in SQL Server 2012 that I would like to add a column to each that will populate with the record creation date and time when I write new records to the table. How can this be done?
Avatar of Ross Turner
Ross Turner
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of Aneesh
ALTER TABLE tableName ADD ColumnName datetime2 default sysdatetime()
Avatar of DB-aha
DB-aha

ASKER

I found one way that requires me to rebuild the tables. As of now this is ok because there is no data in the tables, but I would need to know another way that would allow for this change when data is present. Thanks

CREATE TABLE Example
(
SomeField INTEGER,
DateCreated DATETIME NOT NULL DEFAULT(GETDATE())
)
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DB-aha

ASKER

This solution had the added NOT NULL that makes life so much easier. Thanks for the resolution.