script to restore database sql server from bak file, doesn't work

陌路散爱 提交于 2019-12-03 15:18:26

问题


I have an empty database:

DB_Clients

And I want to restore the database from a .bak file:

OldDBClients.bak

This is the path:

C:\OldDBClients.bak

And this is my script:

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

When I execute it, I get this error message:

Msg 3154, Level 16, State 4, Line 15
The backup set holds a backup of a database other than the existing 'DB_Clients' database.
Msg 3013, Level 16, State 1, Line 15
RESTORE DATABASE is terminating abnormally.

Can someone tell me why this happen? I have to point that the file has the permissions to read and write.

Thank's.


回答1:


You need to use WITH REPLACE option in order to overwrite the existing database.

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

Probably you also need to specify WITH MOVE options; in this case:

  • use RESTORE FILELISTONLY FROM DISK = 'C:\OldDBClients.bak' to know logical name of your MDF/LDF
  • use WITH MOVE options in your RESTORE

For example:

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

Please note that you can also DROP your empty DB_Clients database and use a simple RESTORE.




回答2:


You should this syntax:

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

It instructs SQL Server to overwrite the existing copy and specifies a valid location for your data and log files




回答3:


Step 1: Check the logical file names with the help of the following command:

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

Step 2: Use the logical names you get from the above query in the below query:

 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'

After running the above commands with the correct values you will see the output like this:

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


来源:https://stackoverflow.com/questions/33783947/script-to-restore-database-sql-server-from-bak-file-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!