Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore Database and change the location for MDF File

I wont restore my database but the location path is not the same. How can i change this path(partition)?

RESTORE DATABASE [MY_DATABASE] 
FROM  DISK = 'C:\Content.bak' 
WITH  FILE = 1,  
NOUNLOAD,  
STATS = 10

Error Message:

Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "F:....\Content01.mdf" failed with the operating system error 3(failed to retrieve text for this error. Reason: 15100).
Msg 3156, Level 16, State 3, Line 1
File 'Content01' cannot be restored to 'F:....\Content01.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "H:....\Content01_log.LDF" failed with the operating system error 3(failed to retrieve text for this error. Reason: 15105).
Msg 3156, Level 16, State 3, Line 1
File 'Content01_log' cannot be restored to 'H:....\Content01_log.LDF'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

THANKS.

like image 513
Hechler Avatar asked Nov 03 '11 12:11

Hechler


People also ask

Can we restore database from MDF file?

Use SQL Repair Tool to Restore Database. If you failed to attach a database due to corrupted or damaged MDF file, use Stellar Repair for MS SQL software to repair the MDF file.

How do I restore a database in a different location?

Restore a database to a new location; optionally rename the database using SSMS. Connect to the appropriate instance of the SQL Server Database Engine, and then in Object Explorer, select the server name to expand the server tree. Right-click Databases, and then select Restore Database.


3 Answers

RESTORE DATABASE [My_Database] 
FROM DISK = 'C:\Content.bak'
WITH MOVE 'MyDatabase_Data' TO 'C:\Data\MyDatabase_Data.mdf',
MOVE 'MyDatabase_Log' TO 'C:\Data\MyDatabase_Log.ldf',
REPLACE,
STATS=10
like image 134
Alex_L Avatar answered Dec 08 '22 11:12

Alex_L


Use the WITH MOVE command of the restore as discussed in this SO Question.

like image 44
Mike Walsh Avatar answered Dec 08 '22 11:12

Mike Walsh


If anyone is here because they are restoring a database with multiple files, each destination file needs a new name. Using SQL Server 2008 R2, the gui does not provide an obvious clue nor does it solve it automatically.

like image 38
Josh-LastName Avatar answered Dec 08 '22 12:12

Josh-LastName