Link to home
Start Free TrialLog in
Avatar of b001
b001Flag for Afghanistan

asked on

sql server 2008 tables

Hi Experts
I have a Sql Database called Master
and it has more than 200 tables.

 I would to run a query to find the names of all table been updated today.

Thanks
Avatar of TempDBA
TempDBA
Flag of India image

Table been updated today. I am afraid you can't do that unless you have some manual auditing\CDC,etc is applied. If you have datetime field for all the tables where you have specified the date of data insertion you can do that, or if you have trigger that logs data with datetime somewhere, or if you have inbuilt mechanism like CDC from SQL server is in practise. Else you can't do that.
Avatar of lludden
Updated as in the table structure has been updated, or values within the table?

For the latter, add this trigger in to each table you want to track.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[LogUpdatedTimes_<TableName>] ON [dbo].<TableName> FOR INSERT, UPDATE, DELETE AS
BEGIN
      SET NOCOUNT ON

      MERGE INTO SQLTableUpdateTimes AS Target USING (
                                                                              SELECT '<TableName>' AS TableName,
                                                                                       getdate() AS CurrentUpdate
                                                                           ) AS Source ON Source.TableName = Target.TableName WHEN MATCHED THEN UPDATE
      SET Target.LastUpdate = getdate()
      WHEN NOT MATCHED THEN INSERT (Tablename, LastUpdate)
      VALUES (Source.TableName,
                  Source.CurrentUpdate);

END

and add this table:
CREATE TABLE [dbo].[SQLTableUpdateTimes](
      [TableName] [varchar](50) NOT NULL,
      [LastUpdate] [datetime] NOT NULL,
 CONSTRAINT [PK_SQLTableUpdateTimes] PRIMARY KEY CLUSTERED
(
      [TableName] ASC
)
ASKER CERTIFIED SOLUTION
Avatar of mark_gao
mark_gao

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial