About Me

My photo
a Dynamic and Energetic guy.....

Thursday, July 11, 2013

Getting exclusive access to a SQL Server database for restore - Solution

*** When we restore DB we are always getting exclusive access error.

So I have written a Query to
set DB OFFLINE
RESTORE DB
Changing location of .MDF and .LDF files

ALTER DATABASE WSS_CONTENT_1545 SET OFFLINE WITH ROLLBACK IMMEDIATE

--ALTER DATABASE WSS_CONTENT_1545 SET ONLINE

USE[MASTER]
ALTER DATABASE WSS_CONTENT_1545
SET SINGLE_USER WITH ROLLBACK immediate

Restore database WSS_CONTENT_1545
from Disk = 'E:\Wss_Content_Bak.bak' with REPLACE,
move 'WSS_Content_1111' to 'E:\Intra\WSS_Content_1545.mdf',
move 'WSS_Content_1111_Log' to 'E:\Intra\WSS_Content_1545.ldf'

*** (WSS_Content_1111 is the OLD DATABASE NAME)



My Masters