从bak文件恢复数据库sql server的脚本,不起作用

时间:2015-11-18 15:25:08

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

我有一个空数据库:

DB_Clients

我想从.bak文件恢复数据库:

OldDBClients.bak

这是路径:

C:\OldDBClients.bak

这是我的剧本:

USE [master]
GO
    RESTORE DATABASE DB_Clients
    FROM DISK = 'C:\OldDBClients.bak'

当我执行它时,我收到此错误消息:

  

Msg 3154,Level 16,State 4,Line 15
  备份集包含除现有' DB_Clients'之外的数据库的备份。数据库。
  消息3013,16级,状态1,行15   RESTORE DATABASE异常终止。

有人能告诉我为什么会这样吗? 我必须指出该文件具有读写权限。

感谢' S

3 个答案:

答案 0 :(得分:13)

您需要使用WITH REPLACE选项才能覆盖现有数据库。

RESTORE DATABASE DB_Clients
FROM DISK = 'C:\OldDBClients.bak'
WITH REPLACE

您可能还需要指定WITH MOVE个选项;在这种情况下:

  • 使用RESTORE FILELISTONLY FROM DISK = 'C:\OldDBClients.bak'了解您的MDF / LDF的逻辑名称
  • 在RESTORE中使用WITH MOVE选项

例如:

RESTORE DATABASE DB_Clients
FROM DISK = 'C:\OldDBClients.bak'
WITH REPLACE,
WITH MOVE 'YourMDFLogicalName' TO '<MDF file path>',
MOVE 'YourLDFLogicalName' TO '<LDF file path>'

请注意,您还可以DROPDB_Clients个数据库并使用简单的RESTORE

答案 1 :(得分:7)

您应该使用以下语法:

USE [master]
GO
RESTORE DATABASE DB_Clients FROM DISK = 'C:\OldDBClients.bak' WITH 
MOVE 'DB_Clients' TO 'D:\SQLServer\Data\DB_Clients.mdf',
MOVE 'DB_Clients_log' TO 'D:\SQLServer\Log\DB_Clients.ldf', REPLACE

它指示SQL Server覆盖现有副本并为数据和日志文件指定有效位置

答案 2 :(得分:0)

步骤1: 借助以下命令检查逻辑文件名:

RESTORE FILELISTONLY 
FROM DISK = 'E:\DBBackups\mydb.bak'

第2步:在以下查询中使用从上述查询中获得的逻辑名:

 RESTORE DATABASE [mydb_new]
  FILE = N'<MDFLogicalName>'
  FROM DISK = N'E:\DBBackups\mydb.bak'
  WITH 
    FILE = 1, NOUNLOAD, STATS = 10,
    MOVE N'<MDFLogicalname>'
    TO N'E:\DBBackups\mydb_new.mdf',
    MOVE N'<LDFLogicalName>'
    TO N'E:\DBBackups\mydb_new_0.ldf'

以正确的值运行上述命令后,您将看到如下输出:

10 percent processed.
20 percent processed.
30 percent processed.
40 percent processed.
50 percent processed.
60 percent processed.
70 percent processed.
80 percent processed.
90 percent processed.
100 percent processed.
Processed 7672 pages for database 'mydb_new', file '<MDFLogicalname>' on file 1.
Processed 5 pages for database 'mydb_new', file '<LDFLogicalName>' on file 1.
RESTORE DATABASE ... FILE=<name> successfully processed 7677 pages in 0.780 seconds (76.893 MB/sec).

Completion time: 2019-10-20T11:35:31.8343787+05:30