Avatar of brasso_42
brasso_42

asked on 

MSSQL 2012 - Date a record was added

Hi

Is there any way to find out what date a record was added to a table other than adding a date field and putting the date in there at the point of creating the record?

e.g table1
Name          Tel_No
Fred             1234
Bob              4321

How can I tell when bob was added to the table?

Thanks
Microsoft SQL Server

Avatar of undefined
Last Comment
Scott Pletcher
Avatar of Phillip Burton
Phillip Burton

alter table table1
add MyDate datetime constraint rightnow default GETDATE()

Open in new window


It is not retrospective, but when add the current date and time when a new record is entered.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

You can also log this outside of the main table itself.  I prefer that, although developers don't, in my experience.

Developers love to put all those extra columns in *every* table, but they obviously increase the row size, sometimes drastically (on some intersection tables, the create & update cols are more bytes than the actual data in the row!).  

If you're going to add these types of columns in the table itself, at least use an int id rather than varchar(), by creating a separate lookup table for the names.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

In theory, if the table uses an identity column, you could also get the date by simply capturing the id number every day at midnight, then looking up the id number to see which day range it falls into.  For more precise time, you could capture the identity value every hour, or even every minute.

Of course if the identity value gets reset you could have issues, but many tables will truly never do that because it would also destroy the app itself.  If they're truly being used, the new values would simply replace the old values in the id-->date conversion table.
Avatar of brasso_42
brasso_42

ASKER

Thanks for your help!
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Scott - I created a new question asking about the better ways to handle these 'auditing' columns, per your comments.  I'm genuinely interested in how you're currently pulling this off.  Thanks.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

I'll Monitor the new q for a while before I say anything, to allow others to present their thoughts on this.  I suspect strongly that DBAs and developers will see this q quite differently :).

Btw, I am on the DBA side, and thus against "automatically" adding ~40 bytes and two triggers to every table :).
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo