Monday, September 9, 2013

SQL 2008 - The database is set to single user mode and a user is accessing it

Problem:
You set your database to single user mode in the hopes of restoring a company, but then it hangs, or some other process grabs your single login, and you can't get back in to do anything.

Solution:

--Find the spid of the user logged into your company and kill it
-------------------------------------------------------------------------
use master
select * from master.sys.sysprocesses
where spid>50
and dbid = db_id('TWO')
-------------------------------------------------------------------------
KILL 59  -- or whatever spid comes up in the results
-------------------------------------------------------------------------
--You can set the database back to multi user mode
------------------------------------------------------------------------
alter database [TWO] set multi_user with rollback immediate

--Or you can go ahead with your original restore script and just restore over it immediately.
--Always try to run all of these activities in a single script so any automated processes don't get a chance to
--grab the connection again
-----------------------------------------------------------------------



RESTORE DATABASE [CILT] FROM  DISK = N'D:\Live.bak' WITH  FILE = 1,  MOVE N'GPSLiveDat.mdf' TO N'D:\GPSTestDat.mdf',  MOVE N'GPSLiveLog.ldf' TO N'D:\GPSTestLog.ldf',  NOUNLOAD,  REPLACE,  STATS = 10
GO

No comments:

Post a Comment