Move a file to another location in SQL Server
Here is the procedure to move a file to another location (e.g. for the database called MyDB).
First you need to execute the first part of this snippet (until the comment '/**** Now you need to physically move the file to the other location ***/').
Then you need to move the file and eventually you can execute the last two statements.
post this code
ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE MyDB SET OFFLINE;
ALTER DATABASE MyDB MODIFY FILE
Name = MyDB_file1,
Filename = 'c:\DB\MyDB.mdf'
/**** Now you need to physically move the file to the other location ***/
ALTER DATABASE MyDB SET ONLINE;
ALTER DATABASE MyDB SET MULTI_USER;
If you want to be updated about similar snippets,
and follow our Channels