Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 15:49
Show Gist options
  • Save Otterpohl/15e80f6d0c84c97adb726afe8e82d840 to your computer and use it in GitHub Desktop.
Save Otterpohl/15e80f6d0c84c97adb726afe8e82d840 to your computer and use it in GitHub Desktop.
Get the most recent database restore detail
WITH LastRestores AS
(
SELECT [rs].[destination_database_name]
,[rs].[restore_date]
,[bs].[backup_start_date]
,[bs].[backup_finish_date]
,[bs].[database_name] AS [source_database_name]
,[bmf].[physical_device_name] AS [backup_file_used_for_restore]
,RowNum = ROW_NUMBER() OVER (PARTITION BY [rs].[destination_database_name] ORDER BY rs.[restore_date] DESC)
FROM msdb..restorehistory rs
INNER JOIN msdb..backupset bs ON [rs].[backup_set_id] = [bs].[backup_set_id]
INNER JOIN msdb..backupmediafamily bmf ON [bs].[media_set_id] = [bmf].[media_set_id]
)
SELECT *
FROM [LastRestores]
WHERE [RowNum] = 1
ORDER BY restore_date DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment