使用所有数据还原数据库

时间:2018-06-04 06:55:05

标签: sql sql-server sql-server-2008 database-restore

我创建了数据库的完整备份(.bak文件)。然后,我将它恢复到另一台机器。我使用此查询来恢复:

IF DB_ID('Northwind') IS NULL 
BEGIN
RESTORE DATABASE [Northwind]
FILE = N'Northwind_Data'
FROM DISK = N'C:\Program Files\Microsoft SQLServer\MSSQL10_50.SS2008\MSSQL\Backup\Northwind.bak'
WITH 
   FILE = 1, NOUNLOAD, STATS = 10,
   MOVE N'YOUR logical name of data file as shown by RESTORE FILELISTONLY command'
        TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SS2008\MSSQL\DATA\Northwind.mdf',
   MOVE N'YOUR logical name of Log file as shown by RESTORE FILELISTONLY command'
        TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SS2008\MSSQL\DATA\Northwind_0.LDF'
END

但是当我使用select查询时,数据仍然显示与恢复前相同。 如果我希望所有数据彼此匹配,如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

我认为您的.Bak文件存在问题。尝试通过SSMS恢复。

打开SSMS,单击“数据库”,然后选择“还原” enter image description here

从设备中选择 enter image description here

选择.BAK文件 enter image description here

答案 1 :(得分:0)

提供的脚本具有IF条件,该条件检查DB_ID系统函数以查看SQL Server上是否存在具有相同名称的任何数据库。如果check为true,则跳过还原过程。不应该是SQL Server上具有相同名称的数据库,以便运行此脚本。

或者您可以修改脚本以包含用于还原数据库的REPLACE选项。请参阅MSDN链接:https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-2017#REPLACEoption

您的脚本现在应如下所示:

   IF DB_ID('Northwind') IS NULL 
BEGIN  //This runs if no database of the name "Northwind" exist
RESTORE DATABASE [Northwind]
FILE = N'Northwind_Data'
FROM DISK = N'C:\Program Files\Microsoft SQLServer\MSSQL10_50.SS2008\MSSQL\Backup\Northwind.bak'
WITH 
   FILE = 1, NOUNLOAD, STATS = 10, 
   MOVE N'YOUR logical name of data file as shown by RESTORE FILELISTONLY command'
        TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SS2008\MSSQL\DATA\Northwind.mdf',
   MOVE N'YOUR logical name of Log file as shown by RESTORE FILELISTONLY command'
        TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SS2008\MSSQL\DATA\Northwind_0.LDF'
END
ELSE
BEGIN //This runs if there is a database with same name by replaceing it.
RESTORE DATABASE [Northwind]
FILE = N'Northwind_Data'
FROM DISK = N'C:\Program Files\Microsoft SQLServer\MSSQL10_50.SS2008\MSSQL\Backup\Northwind.bak'
WITH 
   FILE = 1, NOUNLOAD, REPLACE, STATS = 10, 
   MOVE N'YOUR logical name of data file as shown by RESTORE FILELISTONLY command'
        TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SS2008\MSSQL\DATA\Northwind.mdf',
   MOVE N'YOUR logical name of Log file as shown by RESTORE FILELISTONLY command'
        TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SS2008\MSSQL\DATA\Northwind_0.LDF'
END

有很棒的第三方工具可以执行这些备份和恢复,甚至可以轻松修复损坏的数据库文件,以帮助您完成工作。查看Stellar Database Toolkit

注意:Stack Overflow有一个与此分开的数据库管理员论坛https://dba.stackexchange.com/,针对此类数据库问题。在那里发布数据库相关问题,以获得更快更准确的答案。

谢谢和HTH。