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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 TimeStamp that Sticks

Author  Topic 

PHolt
Starting Member

3 Posts

Posted - 2009-06-10 : 15:03:32
I've created a field in a table wherein when a record gets added to the table, a timestamp will note the time at which the record was added. I have this working in SQL 2000 by just adding (getdate()) to the field, but it doesn't seem to work in SQL 2005. The (getdate()) is in the field but it's recording the current time for ALL records in the table, i.e., every record has the same time.

I checked the forums for this topic but couldn't find anything, if there is a topic similar to this, that would help. Thanks.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-10 : 15:10:43
Can you show us a little bit of your code?
GETDATE() is in the field says nothing ...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

PHolt
Starting Member

3 Posts

Posted - 2009-06-10 : 15:22:49
There is no code, it's essentially a table with fields. In the field called EntryDate, I have put the command (getdate()) in the Computed Column Specification under the Table Designer when you go to modify the field using SQL Server Management Studio.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-10 : 15:50:01
Computed column values are calculated every time they are required by a query.
Thats why this cannot work.
You can use a trigger to populate EntryDate with a getdate() on insert.

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

lynda
Starting Member

21 Posts

Posted - 2009-06-10 : 15:50:36
Add (GETDATE()) to the default value spec for that column.
Go to Top of Page

PHolt
Starting Member

3 Posts

Posted - 2009-06-11 : 11:00:08
Thanks lynda! That did the trick!
Go to Top of Page
   

- Advertisement -