Using SQL Server, how can I obtain the restore date for a particular database?
3 Answers
It's a bit difficult to determine exactly what you want from the way your question is worded, but it sounds like this post may give you the needed information
Identify when a SQL Server database was restored, the source and backup date
From the post:
After restoring a database your users will typically run some queries to verify the data is as expected. However, there are times when your users may question whether the restore was done using the correct backup file. In this tip I will show you how you can identify the file(s) that was used for the restore, when the backup actually occured and when the database was restored.
Here's the script from that post:
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]
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]
ORDER BY [rs].[restore_date] DESC
- 24,062
- 2
- 29
- 52
You can find this information easily using dbatools' Get-DbaRestoreHistory. Works for one or all databases and supports SQL Server 2005-vNext.
Get-DbaRestoreHistory -SqlInstance sql2005 -Database db_2005_CL80 | Select -Last 1
Here's some sample output
- 254
- 1
- 4
According to the link below:
WITH LastRestores AS
(
SELECT
DatabaseName = [d].[name] ,
[d].[create_date] ,
[d].[compatibility_level] ,
[d].[collation_name] ,
r.*,
RowNum = ROW_NUMBER() OVER (PARTITION BY d.Name ORDER BY r.[restore_date] DESC)
FROM master.sys.databases d
LEFT OUTER JOIN msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.Name
)
SELECT *
FROM [LastRestores]
WHERE [RowNum] = 1
- 1,126
- 1
- 10
- 31
