{"id":9034,"date":"2022-07-22T06:52:00","date_gmt":"2022-07-22T04:52:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=9034"},"modified":"2022-08-04T14:17:38","modified_gmt":"2022-08-04T12:17:38","slug":"last-time-table-was-updated-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/last-time-table-was-updated-sql-server\/","title":{"rendered":"Last time table was updated in SQL Server"},"content":{"rendered":"\n<p>How to check the last date a table was updated on a SQL Server database? Check the last access and update with a simple SQL query? Thanks to the SQL Server relational database management system tables, a query allows you to display the last changes done. We can apply it to any SQL Server table or view, like last scans, or last updates for example.&nbsp;<\/p>\n\n\n\n<p>This feature is especially useful for audit purpose or database troubleshooting, after data integration for example. The query shows also the number of updates made and the last update time&nbsp; for the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-the-last-date-and-time-a-table-was-updated-in-sql-server\">Get the last date and time a table was updated in SQL Server<\/h2>\n\n\n\n<p>Indeed, the goal here is to know when a specific table has been updated. For exemple when the SQL framework in place do not update metadata like insert date or update. <\/p>\n\n\n\n<p>Please note that the metadata is something highly recommended especially with sensitive data and large amount of data. <\/p>\n\n\n\n<p>The SQL query uses the table <strong>sys.tables<\/strong> and the <strong>sys.dm_db_index_usage_stats<\/strong> system tables.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT\t\n    object_name(STAT.object_id) AS ObjectName, \n    USER_SEEKS,\n    USER_SCANS,\n    USER_LOOKUPS,\n    USER_UPDATES,\n    LAST_USER_SEEK,\n    LAST_USER_SCAN,\n    LAST_USER_LOOKUP,\n    LAST_USER_UPDATE\nFROM\tSYS.DM_DB_INDEX_USAGE_STATS STAT JOIN\n        SYS.TABLES TAB ON (TAB.OBJECT_ID = STAT.OBJECT_ID)\nWHERE \tDATABASE_ID = DB_ID();\n<\/pre>\n\n\n\n<p>In this example, let us consider only the sales table, and the number of access made is one.<\/p>\n\n\n\n<p>I.e., for reading the data, the information is in the column &#8220;<strong>Last User Scan<\/strong>&#8220;. We also see one update in the column named &#8220;<strong>Last User Update<\/strong>&#8220;.<\/p>\n\n\n\n<p>To achieve this result, very useful in some cases, the example uses the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/system-dynamic-management-views\/sys-dm-db-index-usage-stats-transact-sql?view=sql-server-ver15\" target=\"_blank\" rel=\"noopener noreferrer\">SYS.DM_DB_INDEX_USAGE_STATS<\/a> system table.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/expert-only.net\/wp-content\/uploads\/2017\/06\/sql-server-table-query-access-update-stats-1.jpg\" alt=\"Get the last date a table was updated on SQL Server\" class=\"wp-image-452\"\/><figcaption><strong>Get the last date a table was updated on SQL Server<\/strong><\/figcaption><\/figure>\n\n\n\n<p>As per the screenshot, it also displays the seeks, the scans and lookups. Please note that these tables contains many other useful statistics on table.<\/p>\n\n\n\n<p>This article explains how to display the last access and update date to an a given SQL Server table. This SQL code helps for compliance and troubleshooting. <\/p>\n\n\n\n<p>Check this other article to learn how to <a href=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-tables\/\">display manage SQL Server tables with T-SQL scripts<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tutoriels-sql-et-it wp-block-embed-tutoriels-sql-et-it\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-tables\/\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to check the last date a table was updated on a SQL Server database? Check the last access and update with a simple SQL query? Thanks to the SQL Server relational database management system tables, a query allows <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/last-time-table-was-updated-sql-server\/\" title=\"Last time table was updated in SQL Server\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":5509,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-9034","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=9034"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9034\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/5509"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=9034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=9034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=9034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}