Friday, September 16, 2016

Change Drive location of MS Sql

USE MASTER;
GO
-- Take database in single user mode -- if you are facing errors -- This may terminate your active transactions for database

ALTER DATABASE xxDBNamexx
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db @dbname = N'TestDB' GO

Now move the files from loc1 to loc2. You can now reattach the files with new locations

-- Move MDF File from Loc1 to Loc 2
-- Re-Attached DB
CREATE DATABASE [TestDB] ON
( FILENAME = N'F:\loc2\TestDB.mdf' ),
( FILENAME = N'F:\loc2\TestDB_log.ldf' )
FOR ATTACH
GO

No comments:

Post a Comment