我的所有表格中都有一个Modified_Dt
类型Datetime
的字段,用于跟踪记录的上次修改日期和时间。
现在,让我们说我需要知道哪些表有最近修改过的记录(比如今天)。
如何为此编写查询?如何查询多个表?
顺便说一下,我正在使用MS SQL Server 2008 R2
。
答案 0 :(得分:2)
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
注意:将“DBStackExchange”替换为您的数据库名称
答案 1 :(得分:1)
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)
答案 2 :(得分:1)
这是没有光标或临时表的答案
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