Click here to Skip to main content
15,868,016 members
Articles / Database Development / SQL Server

Solution: The WITH MOVE Clause Can Be Used to Relocate One or More Files

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
9 Jan 2014CPOL 72.4K   4
The WITH MOVE clause can be used to relocate one or more files

Today, while working with SQL Server, I was trying to restore a DB from .bak file and got a strange error while restoring using the normal procedure. I was working on SQL Server 2008 R2 and the backup was from an old version making me think the problem may be because of incorrect SQL versions. The error read as below:

SQL
TITLE:
Microsoft SQL Server Management Studio
——————————
Restore failed for Server ‘MYSQLSERVER’. (Microsoft.SqlServer.SmoExtended)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.4000.0+
((KJ_PCU_Main).120628-0827+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.
FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476

——————————

ADDITIONAL INFORMATION:

System.Data.SqlClient.SqlError: 
File ‘C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDatabase.mdf’ 
is claimed by ‘MyDatabase_log’(2) and ‘MyDatabase’(1). 
The WITH MOVE clause can be used to relocate one or more files. (Microsoft.SqlServer.Smo)

For help, click http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.4000.0+((KJ_PCU_Main).120628-0827+)&LinkId=20476.

As usual, the links provided by SQL Server were of least use. After reading the error closely a few times, I understood the error is not related to SQL Server version but is related to the files being used by the restoration. It was found that the .mdf file of the database is being used twice by the restoration process and hence tried to restore the database using a script. Below is the solution to restore the database using a script.

SQL
RESTORE DATABASE [MyDatabase]
FROM DISK = N'E:\MyDatabase.bak'
WITH FILE = 1,
MOVE N'MyDatabase'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\MyDatabase.mdf',
MOVE N'MyDatabase_Log'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\MyDatabase.ldf',
NOUNLOAD, REPLACE, STATS = 10
GO

Using the script and changing proper values for your database, you will be able to restore the database with ease. Hope you like this post! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

Love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks. #SOreadytohelp

Comments and Discussions

 
PraiseSaved me a headache -Thanks! Pin
SwankIBS15-Mar-16 4:35
SwankIBS15-Mar-16 4:35 
Never had this happen before today... but your solution worked! Thanks so much.
GeneralRe: Saved me a headache -Thanks! Pin
Nitesh Kejriwal21-Mar-16 2:37
professionalNitesh Kejriwal21-Mar-16 2:37 
AnswerWorked for me! Pin
Member 1178409522-Jun-15 8:07
Member 1178409522-Jun-15 8:07 
GeneralRe: Worked for me! Pin
Nitesh Kejriwal22-Jun-15 8:15
professionalNitesh Kejriwal22-Jun-15 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.