Created
October 4, 2024 09:51
-
-
Save ghotz/7ac80734f66a9535225506d0b1460a74 to your computer and use it in GitHub Desktop.
Get last fully restored databases info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH cte AS | |
( | |
SELECT | |
RH.destination_database_name, RH.restore_type | |
, ROW_NUMBER() OVER (PARTITION BY RH.destination_database_name, RH.restore_type ORDER BY RH.restore_history_id DESC) AS rn | |
, RH.restore_date, BS.backup_start_date, BS.backup_finish_date | |
, CASE WHEN CONVERT(varchar, DATEDIFF(ms, backup_start_date, backup_finish_date) / 1000 / 86400) > 0 THEN CONVERT(varchar, DATEDIFF(ms, backup_start_date, backup_finish_date) / 1000 / 86400) + ' day(s) ' ELSE '' END + CONVERT(varchar, DATEADD(ms, DATEDIFF(ms, backup_start_date, backup_finish_date), 0), 114) AS elapsed_time | |
, FORMAT(BS.backup_size / 1073741824., 'N2') AS backup_size_gb | |
, FORMAT(BS.compressed_backup_size / 1073741824., 'N2') AS compressed_backup_size_gb | |
FROM msdb.dbo.restorehistory AS RH | |
JOIN msdb.dbo.backupset AS BS | |
ON RH.backup_set_id = bs.backup_set_id | |
) | |
SELECT * | |
FROM cte AS BS1 | |
WHERE BS1.restore_type = 'D' AND BS1.rn = 1 -- most recent FULL backupset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment