Friday, March 23, 2012

Last time table was updated?

Not sure if this is right place to post this but hopefully someone can help me anyways. The problem I have is the database I am using is populated by dozens of tables, my program doesn't create these tables but I need to find an easy way to determine when the last INSERT was done on these tables to see which one is the currently active table. I can't seem to find in MSSQL a last updated datetimestap on the tables, is there a way to easily do this, I don't think open each and every table and than finding the timestamp on the last entry is going to work these tables can get rather large and there could be many of them I think it will take too long to accomplish that way.

Hi Mitch,

Without there being a timestamp or datetime column on the actual table, there's no way for you to dynamically determine the date/time a row was inserted.

You can, however query the system catalog to determine the date the table was created.

As you mention that the tables themselves are created by an application, obviously you'd be hesitant to make any changes to the existing schema; you could, however create generic triggers on each of the tables in question that would in turn insert a flag into a control table to indicate the current "active" table"

CREATE TRIGGER tr_TableHere_ins
ON TableSourceA
FOR INSERT
AS
INSERT INTO ControlTable (InsertDate, TableSource)
VALUES(GetDate(), 'TableSourceA' )

Or Similar...

Cheers,
Rob

No comments:

Post a Comment