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)
 Default values

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-09-10 : 11:57:29
Hi,

In SQL 2005 is it possible to specify a dynamic default value, for example can I set it to be the current date and time when the row was created.

Also could I set it to the date/time when the row was updated? Or would I use a trigger for this?

Thanks

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-10 : 12:01:41
Yes you can set a default constraint on any column with getdate().



Future guru in the making.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-10 : 12:36:26
"Also could I set it to the date/time when the row was updated? Or would I use a trigger for this?"

You can set a column datatype to be ROWVERSION (formerly the rather confusingly named TIMESTAMP) which is basically an integer which is guaranteed to be changed whenever a row is updated. This is useful for saying things like "Has the data changed since the copy I have of it?" (a good use is for a pessimistic locking system). but if you actually want the Date/Time of the update then you'll need a Trigger.

Beware that using a trigger will re-date data when it is "imported", and for that reason we set the UpdateDate on our table-rows using the Stored Procedure or SQL which updates the table; that way we can import data from "other databases" retaining the "Last Changed" date/time from the source, so it does not become "date/time last imported"

Kristen
Go to Top of Page
   

- Advertisement -