Automatically Insert A DateTime Stamp On Insert

Here is one I don’t use often and sometimes forget.  So I’ll put it out here.

Normally I do my inserts via .NET or a stored proc and so I do my datetime stamps that way.  What if I do not want to do that, surely there is a way to do it inside the table.  Well, you are right and it is really very easy.  In your table design simply set the default value to (getdate()).

I normally use the visual way of creating my tables so I’ll include a screen shot below.  To show how it is done in code I will paste that after the image.

ALTER TABLE [dbo].[tblMyTable] ADD  CONSTRAINT [DF_tblMyTable_dtImported]  DEFAULT (getdate()) FOR [dtImported]
Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on August 29, 2012, in SQL and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: