简体   繁体   中英

List tables with recently modified records

I have a field namely Modified_Dt of type Datetime in all of my tables, to keep track of last modified date and time for a record.

Now, let's say I need to know which tables has records that has been modified recently(like today).

How do I write a query for that? How do I query multiple tables?

By the way, I am using MS SQL Server 2008 R2 .

USE MASTER  
GO  

DECLARE @ObjectName NVARCHAR(255)  

DECLARE TablesList CURSOR  
    FOR select  object_name(object_id, db_id('DBStackExchange'))  
        from    [DBStackExchange].sys.columns  
        where   name = 'Modified_Dt'  

OPEN TablesList  
FETCH NEXT FROM TablesList INTO @ObjectName  
WHILE @@FETCH_STATUS = 0    
    BEGIN  

        exec  
            ( 'If exists ( SELECT 1 FROM DBStackExchange.dbo.[' + @ObjectName  
              + ']  
                   Where     convert(varchar(20),Modified_Dt,103)>=convert(varchar(20),getdate(),103))  

               Print ''' + @ObjectName + '''  
              '
            )  

        FETCH NEXT FROM TablesList INTO @ObjectName  
    END  
CLOSE TablesList  
DEALLOCATE TablesList  

Note: Replace 'DBStackExchange' with your Database name

declare @T table (T_Name nvarchar(255), M datetime)

declare @T_Name nvarchar(255), @SQLT nvarchar(max)
declare c cursor for select name from sys.tables
open c
fetch next from c into @T_Name
while @@fetch_status = 0 begin
    set @SQLT = 'select top 1 ''' + @T_Name + ''', Modified_Dt from ' + @T_Name + ' order by Modified_Dt desc'
    insert @T
    exec sp_executesql @SQLT
    fetch next from c into @T_Name
end
close c
deallocate c

select * from @T where M >= dateadd(day,datediff(day,0,getdate()),0)

Here is an answer without cursor or temporary table

DECLARE @ColumnName AS nvarchar(40) = 'Modified_Dt';
DECLARE @ModifiedSince AS datetime = '20140709';
DECLARE @sql AS nvarchar(max) = '';

-- Build a query with UNION ALL between all tables containing @ColumnName
WITH AllTables AS (
    SELECT SCHEMA_NAME(Tables.schema_id) AS SchemaName
          ,Tables.name AS TableName
          ,Columns.name AS ColumnName
    FROM sys.tables AS Tables
        INNER JOIN sys.columns AS Columns 
            ON Tables.object_id = Columns.object_id
    WHERE Columns.name = @ColumnName
)
SELECT @sql = @sql + 
             'UNION ALL SELECT ' + QUOTENAME(TableName, '''') + 
                            ', ' + QUOTENAME(ColumnName) + 
                      ' FROM ' + QUOTENAME(TableName) + CHAR(13)
FROM AllTables;

-- Create a query which selects last change from all tables
SET @sql = 
'WITH AllChanges(TableName, ModifiedTime) AS ( ' +
STUFF(@sql, 1, LEN('UNION ALL'), '') + -- Remove first UNION
') ' +
'SELECT TableName ' +
'      ,MAX(ModifiedTime) ' +
'FROM AllChanges ' +
'WHERE ModifiedTime > @ModifiedSince ' 
'GROUP BY TableName ' 

EXECUTE sp_executesql @sql, N'@ModifiedSince datetime', @ModifiedSince

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM