How can I capture the time at which a record was added to the database – effortlessly. I am using this
create table YourTable
(
Created datetime default getdate()
)
ANy other alternatives?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Still in the vein of using a default constraint… there are other values you can consider using — different advantages to each (involving universal time, precision, etc.).
http://msdn.microsoft.com/en-us/library/ms188383.aspx
Also — consider the size of your data type — datetime is 8bytes — you could define the column as smalldatetime and improve that to 4 bytes (or in 2008, just plain old date, which is 3bytes — though you might actually like knowing the time as well).
Triggers are also an option, but not preferable IMO — for one thing, they can be rolled-back if any constraints are violated (such as external relationship to a table you just created, forgetting about the trigger — oops!)