SQL SERVER – Get Last Restore Date

I report Restore Date from the latest retained msdb history row. My earlier query returned every matching entry.

The uppermost retained restoration folio is examined beside older preserved records.

SELECT d.name AS DatabaseName,d.create_date,d.compatibility_level,
       rh.restore_date,rh.restore_history_id,rh.restore_type
FROM sys.databases d
OUTER APPLY (SELECT TOP (1) r.restore_date,r.restore_history_id,r.restore_type
 FROM msdb.dbo.restorehistory r WHERE r.destination_database_name=d.name
 ORDER BY r.restore_date DESC,r.restore_history_id DESC) rh
ORDER BY d.name;
-- Separate, approved history retention action only:
-- EXEC msdb.dbo.sp_delete_database_backuphistory @database_name=N'YourDatabase';

OUTER APPLY retains databases with no recorded restore as NULL history. Date and history ID define the latest row. It can represent full, differential, log or another restore type. It is not necessarily final recovery or backup creation.

History can be purged. It can also describe an earlier database with the same destination name. Correlate it with the actual recovery evidence.

The commented cleanup procedure is a separate retention operation. It deletes backup and restore metadata, rather than physical backup files. Preserve incident and recovery evidence before cleanup.

Reference: Restore-history scope.

Related reading

Retained restore history is not immutable recovery evidence, it is instance metadata with retention and name-reuse limits.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Backup and Restore, SQL Scripts, SQL Server
Previous Post
Reclaim Space After Dropping a Column With DBCC CLEANTABLE
Next Post
SQL SERVER – NOLOCK with DMVs

Related Posts

1 Comment. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.