Tuesday, February 14, 2012

Date/Time Stamp

Good day,
Is there a way to add a field in a SQL table that automatically inserts a
date/time stamp when the record is created?
Thank you in advance...On Feb 27, 10:38=A0pm, PsyberFox <Psyber...@.discussions.microsoft.com>
wrote:
> Good day,
> Is there a way to add a field in a SQL table that automatically inserts a
> date/time stamp when the record is created?
> Thank you in advance...
You can do it by writing a an insert trigger on table.|||... or having a default value for the column. Of course, a default can be overridden by the INSERT
statement.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<vinodkus@.gmail.com> wrote in message
news:746d44f8-b988-4950-b74a-b5d152515abc@.62g2000hsn.googlegroups.com...
On Feb 27, 10:38 pm, PsyberFox <Psyber...@.discussions.microsoft.com>
wrote:
> Good day,
> Is there a way to add a field in a SQL table that automatically inserts a
> date/time stamp when the record is created?
> Thank you in advance...
You can do it by writing a an insert trigger on table.|||"PsyberFox" <PsyberFox@.discussions.microsoft.com> wrote in message
news:9B7F88F8-B885-4A28-979B-9B43385D26FF@.microsoft.com...
> Good day,
> Is there a way to add a field in a SQL table that automatically inserts a
> date/time stamp when the record is created?
Just create it with a default of GETDATE() and make sure your procedure(s)
that insert into it don't specify a value for it
CREATE TABLE test (
dtmTimeStamp DATETIME DEFAULT GETDATE(),
[more columns]
)
If you need it to be 'secure' then as someone else mentioned you'll have to
create triggers to keep the value fixed and/or make sure an explicit value
can't be inserted.

No comments:

Post a Comment