46

I want to retrieve the last time table was updated(insert,delete,update).

I tried this query.

SELECT last_user_update
FROM sys.dm_db_index_usage_stats
WHERE object_id=object_id('T')

but the data there is not persisted across service restarts.

I want to preserve the stats even if the service restarts. How can I achieve it?

1
  • 1
    Just about to ask this myself Apr 18, 2019 at 21:01

7 Answers 7

87

If you're talking about last time the table was updated in terms of its structured has changed (new column added, column changed etc.) - use this query:

SELECT name, [modify_date] FROM sys.tables

If you're talking about DML operations (insert, update, delete), then you either need to persist what that DMV gives you on a regular basis, or you need to create triggers on all tables to record that "last modified" date - or check out features like Change Data Capture in SQL Server 2008 and newer.

6
  • 1
    The solution will work only in the Enterprise, Developer, and Enterprise Evaluation editions. Jul 23, 2013 at 4:28
  • 4
    @RameshDurai: which solution are you talking about? The sys.tables is present in ALL editions of SQL Server. But the CDC (Change Data Capture) might be an Enterprise-only edition feature, that's true
    – marc_s
    Jul 23, 2013 at 4:42
  • 1
    Change Data Capture. Because I need to get the updated time for DML operations. Jul 23, 2013 at 5:24
  • 1
    This isn't table data update time. This gives us table sutructure modification time. If you want to see data edit time; i've added an answer. Nov 3, 2014 at 11:40
  • 2
    For non-default schemaSELECT s.name [schema], t.name [table], t.modify_date FROM sys.tables t INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
    – bvj
    Jan 12, 2017 at 5:41
25

If you want to see data updates you could use this technique with required permissions:

SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'DATABASE')
AND OBJECT_ID=OBJECT_ID('TABLE')
4
  • 13
    This solution does not work if the service is restarted. Nov 3, 2014 at 14:15
  • 4
    This is specifically what OP is trying to avoid.
    – KyleMit
    Dec 28, 2016 at 21:11
  • 1
    The last_user_update date is also updated after unique index violation exception. Is there any way around this?
    – Sheinar
    Feb 27, 2019 at 18:44
  • 1
    I did it. But Showed me 'last_user_update' Column is Null
    – mohammadrg
    May 18, 2020 at 4:25
21

Find last time of update on a table

SELECT
tbl.name
,ius.last_user_update
,ius.user_updates
,ius.last_user_seek
,ius.last_user_scan
,ius.last_user_lookup
,ius.user_seeks
,ius.user_scans
,ius.user_lookups
FROM
sys.dm_db_index_usage_stats ius INNER JOIN
sys.tables tbl ON (tbl.OBJECT_ID = ius.OBJECT_ID)
WHERE ius.database_id = DB_ID()

http://www.sqlserver-dba.com/2012/10/sql-server-find-last-time-of-update-on-a-table.html

4

To persist audit data regarding data modifications, you will need to implement a DML Trigger on each table that you are interested in. You will need to create an Audit table, and add code to your triggers to write to this table.

For more details on how to implement DML triggers, refer to this MDSN article http://msdn.microsoft.com/en-us/library/ms191524%28v=sql.105%29.aspx

2
SELECT so.name,so.modify_date

FROM sys.objects as so

INNER JOIN INFORMATION_SCHEMA.TABLES as ist

ON ist.TABLE_NAME=so.name where ist.TABLE_TYPE='BASE TABLE' AND 

TABLE_CATALOG='DbName' order by so.modify_date desc;

this is help to get table modify with table name

1
  • 2
    This query is just for when a table schema was last modified Feb 10, 2018 at 19:57
-1
SELECT UPDATE_TIME
FROM information_schema.tables WHERE TABLE_SCHEMA = 'your_dbname' AND TABLE_NAME = 'your_tablename'
1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 13, 2021 at 7:37
-2

Why not just run this: No need for special permissions

SELECT
    name, 
    object_id, 
    create_date, 
    modify_date
FROM
    sys.tables 
WHERE 
    name like '%yourTablePattern%'
ORDER BY
    modify_date
1
  • 2
    This isn't table data update time. This gives us table structure modification time. Jul 15, 2019 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.