?
Solved

MSSQL 2012 - Date a record was added

Posted on 2014-11-19
8
Medium Priority
?
124 Views
Last Modified: 2014-11-19
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
0
Comment
Question by:brasso_42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40452053
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.
0
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 1400 total points
ID: 40452077
Philip, that is what he don't want to do
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

You can do it reading the transaction log but for that you'll need a 3rd party software and they aren't cheap.
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 600 total points
ID: 40452303
imo the above two experts nailed it.  The answer is 'No you can't', although there are expensive apps to do it (Vitor's post) and ways to handle it in the future (Phillip's post).

As a general rule any table I design that has data entry, I'll add four 'auditing' columns to it that populate via INSERT and UPDATE triggers:
  created_dt  -  datetime - of the entry
  created_by_id -  varchar(30) - SUSER_NAME() of the person creating it
  last_updated_dt  -  datetime - of any insert-update
  last_updated_by_id - varchar(30) - SUSER_NAME() of the person updating it.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40452771
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.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40452786
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.
0
 
LVL 1

Author Closing Comment

by:brasso_42
ID: 40452850
Thanks for your help!
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40452856
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.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40452866
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 :).
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question