locked
Get timestamp for inserted records RRS feed

  • Question

  • I have a table in my MS SQL Express 2008 R2.

    ID     Name     LastName

    1      roko lab

    2      lalla lallaaaa

    Records are already inside this table.


    Is it possible to get timestamp values for inserted records? I forgot to add timestamp column when I was designing database, and now I need to know when records were inserted?

    Saturday, September 28, 2013 8:45 AM

Answers

  • Is it possible to get timestamp values for inserted records? I forgot to add timestamp column when I was designing database, and now I need to know when records were inserted?

    No.  SQL Server does not automatically maintain a internal timestamp of the record insertion time as that would add unnecessary overhead for applications that don't need it.  It is your responsibility to add such a column and the desired value.  Unfortunately, you won't be able to retroactively get the insert timestamp value.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    • Proposed as answer by Eshani Rao Monday, September 30, 2013 4:21 AM
    • Marked as answer by Candy_Zhou Tuesday, October 8, 2013 9:23 AM
    Saturday, September 28, 2013 12:29 PM

All replies

  • You can use getdate() as default value. Have a look at this link:

    Add default value of datetime field in SQL Server to a timestamp

    ALTER TABLE YourTable 
      ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR YourColumn


    Saeid Hasani, sqldevelop.wordpress.com

    Download Books Online for SQL Server 2012


    • Edited by Saeid Hasani Saturday, September 28, 2013 8:59 AM
    Saturday, September 28, 2013 8:58 AM
  • But this will work only for the future records inserted.

    I need to know timestamp for existing records... is it possible?

    Saturday, September 28, 2013 9:17 AM
  • You can update old records by getdate().

    Saeid Hasani, sqldevelop.wordpress.com

    Download Books Online for SQL Server 2012

    Saturday, September 28, 2013 9:27 AM
  • If I update old records they will get timestamp from now.

    I need to know the history... when were those records inserted...

    Saturday, September 28, 2013 11:19 AM
  • Is it possible to get timestamp values for inserted records? I forgot to add timestamp column when I was designing database, and now I need to know when records were inserted?

    No.  SQL Server does not automatically maintain a internal timestamp of the record insertion time as that would add unnecessary overhead for applications that don't need it.  It is your responsibility to add such a column and the desired value.  Unfortunately, you won't be able to retroactively get the insert timestamp value.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    • Proposed as answer by Eshani Rao Monday, September 30, 2013 4:21 AM
    • Marked as answer by Candy_Zhou Tuesday, October 8, 2013 9:23 AM
    Saturday, September 28, 2013 12:29 PM