查找上次更新表的时间


17

查询:

SELECT 
    name AS TableName, 
    create_date AS CreatedDate, 
    modify_date as ModifyDate 
FROM sys.tables 
order by ModifyDate;

...会告诉我上次创建和修改表的时间(从DDL角度来看)。但是我想知道上一次实际数据是从表中插入还是从表中删除。是否可以在SQL Server中获得此权限?

Answers:


22

您也许可以从中得到一个想法

SELECT last_user_update
FROM   sys.dm_db_index_usage_stats us
       JOIN sys.tables t
         ON t.object_id = us.object_id
WHERE  database_id = db_id()
       AND t.object_id = object_id('dbo.YourTable') 

但其中的数据在服务重新启动后不会保留,并且可能无法满足您的要求(例如DELETE FROM T WHERE 1=0,即使实际上未删除任何行,运行也会更新时间)


有什么方法可以一次对数据库中的所有表(而不是单个表)运行此方法吗?谢谢
SQLBoy '17

3
@SQLBoySELECT last_user_update, t.name FROM sys.dm_db_index_usage_stats us JOIN sys.tables t ON t.object_id = us.object_id WHERE database_id = db_id()
Martin Smith

继唯一索引违例异常之后,还会更新last_user_update日期。有没有办法解决?
АленаШлыкова
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.