简体   繁体   English

T-SQL 数据库还原给使用 SSMS 带来不同的结果

[英]T-SQL Database Restore Gives different Result to using SSMS

I am currently trying to use T-SQL to restore my database using the following code:我目前正在尝试使用 T-SQL 使用以下代码恢复我的数据库:

RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH 
   MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf', 
   MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf',
GO

This works, however, it restores it to the 4th of April which was the second last backup it had.这可行,但是,它将它恢复到 4 月 4 日,这是它拥有的倒数第二个备份。

However, when I SSMS and restore the database, it restores it to the latest backup time which is what I want.但是,当我 SSMS 并恢复数据库时,它会将其恢复到我想要的最新备份时间。

How do I get the T-SQL statement to do the same?如何让 T-SQL 语句做同样的事情?

You can find your backup information through the following script and then you can restore the proper backup file which you want.您可以通过以下脚本找到您的备份信息,然后您可以恢复您想要的正确备份文件。

SELECT  physical_device_name 
FROM msdb.dbo.backupmediafamily 
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id 
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7) 
AND msdb.dbo.backupset.database_name ='database_name'
ORDER BY 
msdb.dbo.backupset.backup_finish_date DESC

Do not forget that BACKUP FILES are virtual disks and in a disk you can put more than one BACKUPS.不要忘记备份文件是虚拟磁盘,在一个磁盘中您可以放置多个备份。 By Default BACKUP to a file when existing, appends the new backup into the file.默认情况下,备份到文件(如果存在),将新备份附加到文件中。 I think you can see the mater in executing the following command:我想你可以在执行以下命令时看到这个问题:

RESTORE HEADERONLY FROM DISK = 'D:\Backup\database_name.bak'

So when you are restoring with this command:因此,当您使用此命令进行恢复时:

RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH 
   MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf', 
   MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf'
GO

You just restore to the first oldest backup, wich is equivalent to this command:您只需还原到第一个最旧的备份,这相当于此命令:

RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH 
   MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf', 
   MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf'
WITH FILE = 1
GO

By using SSMS the assistant try to get the lastest database backup and surely do this command:通过使用 SSMS,助手会尝试获取最新的数据库备份,并且肯定会执行以下命令:

RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH 
   MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf', 
   MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf'
WITH FILE = 2
GO

You can have a look to the script that the SSMS assistant will throw before executing, that I recommand always !您可以查看 SSMS 助手在执行之前将抛出的脚本,我总是推荐!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM