Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Finding Record with Last Modified date in all tables
From Wiki
The query below will print table and the date of last modification for each table that has field like ID (OwnerID, AddressID, etc.) and ModifiedDate. Due to the fact it creates a complex query, the result is limited to only 50 first records
- declare @SQL varchar(max)
- select @SQL = stuff((select top (50) char(13) + char(10) + 'UNION ALL ' +
- 'SELECT ' + quotename(table_schema,'''') + ' as [Schema], ' + quotename(Table_Name,'''') +
- ' as [TableName], max(ModifiedDate) as [LastDate] from ' +
- QUOTENAME(Table_SCHEMA) + '.' +
- quotename(Table_Name) from (select Table_Name, TABLE_SCHEMA
- from Information_Schema.Columns C1 where
- Column_Name Like '%ID' and
- exists (select 1 from Information_Schema.Columns C2
- where C1.Table_Name = C2.Table_Name and
- C2.Column_Name = 'ModifiedDate') group by TABLE_NAME, TABLE_SCHEMA) X order by Table_Name
- for XML PATH(''),type).value('.','varchar(max)'),1,12,'')
- --print @SQL
- execute (@SQL)
For the explanation of XML PATH trick please take a look at this blog Making a list and checking it twice
Alternative traditional loop solution:
- declare @TablesCnt int, @Loop int, @Schema sysname,
- @Table sysname, @SQL nvarchar(max)
- if object_id('tempdb..#ListOfTables') IS NOT NULL
- drop table #ListOfTables
- select identity(int,1,1) as Row, table_schema, Table_Name into #ListOfTables
- from ( select distinct Table_Schema, Table_Name from Information_Schema.Columns C1 where
- Column_Name LIKE '%ID' and
- exists (select 1 from Information_Schema.Columns C2
- where C1.Table_Name = C2.Table_Name and
- C2.Column_Name = 'ModifiedDate')) X order by Table_Name
- set @TablesCnt = @@ROWCOUNT
- --select * from #ListOfTables
- if object_id('Result','U') IS not NULL
- drop table Result
- create table Result (SchemaName sysname, TableName Sysname, LatestDate datetime)
- set @Loop = 1
- while @Loop <=@TablesCnt
- begin
- select @Table = Table_Name, @Schema = Table_schema from #ListOfTables where Row = @Loop
- set @Loop = @Loop + 1
- set @SQL = 'select ' + QUOTENAME(@schema,'''') + ' as [Schema], ' +
- quotename(@Table,'''') + ' as Table_Name, max(ModifiedDate) as LastDate from ' + quotename(@Schema) + '.' + quotename(@Table)
- insert into Result execute (@SQL)
- --execute (@SQL)
- end
- select * from Result
- go
Bellow are comparison results between these two methods and the XML solution performs slightly faster (tested on AdventureWorks database)
- set statistics time off
- set nocount on
- declare @StartTime datetime2 = getdate(), @TimeElapsed int
- declare @TablesCnt int, @Loop int, @Schema sysname,
- @Table sysname, @SQL nvarchar(max)
- if object_id('tempdb..#ListOfTables') IS NOT NULL
- drop table #ListOfTables
- select identity(int,1,1) as Row, table_schema, Table_Name into #ListOfTables from
- (select distinct Table_Schema, Table_Name from Information_Schema.Columns C1 where
- Column_Name LIKE '%ID' and
- exists (select 1 from Information_Schema.Columns C2
- where C1.Table_Name = C2.Table_Name and
- C2.Column_Name = 'ModifiedDate')) X order by Table_Name
- set @TablesCnt = @@ROWCOUNT
- --select * from #ListOfTables
- if object_id('Result','U') IS not NULL
- drop table Result
- create table Result (SchemaName sysname, TableName Sysname, LatestDate datetime)
- set @Loop = 1
- while @Loop <=@TablesCnt
- begin
- select @Table = Table_Name, @Schema = Table_schema from #ListOfTables where Row = @Loop
- set @Loop = @Loop + 1
- set @SQL = 'select ' + QUOTENAME(@schema,'''') + ' as [Schema], ' +
- quotename(@Table,'''') + ' as Table_Name, max(ModifiedDate) as LastDate from ' + quotename(@Schema) + '.' + quotename(@Table)
- insert into Result execute (@SQL)
- --execute (@SQL)
- end
- --select * from Result
- select DATEDIFF(MS, @StartTime, GETDATE())
- go
- -- Test query
- set nocount on
- declare @StartTime datetime2 = getdate(), @TimeElapsed int
- declare @SQL varchar(max)
- if object_id('Result','U') IS not NULL
- drop table Result
- create table Result (SchemaName sysname, TableName Sysname, LatestDate datetime)
- select @SQL = stuff((select char(13) + char(10) + 'UNION ALL ' +
- 'SELECT ' + quotename(table_schema,'''') + ' as [Schema], ' + quotename(Table_Name,'''') +
- ' as [TableName], max(ModifiedDate) as [LastDate] from ' +
- QUOTENAME(Table_SCHEMA) + '.' +
- quotename(Table_Name) from (select Table_Name, TABLE_SCHEMA
- from Information_Schema.Columns C1 where
- Column_Name Like '%ID' and
- exists (select 1 from Information_Schema.Columns C2
- where C1.Table_Name = C2.Table_Name and
- C2.Column_Name = 'ModifiedDate') group by TABLE_NAME, TABLE_SCHEMA) X order by Table_Name
- for XML PATH(''),type).value('.','varchar(max)'),1,12,'')
- --print @SQL
- insert into Result execute (@SQL)
- select DATEDIFF(MS, @StartTime, GETDATE())



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.