5

I have a Database on SQL Server 2008, which I want to drop. Currently it is in single user mode and it is currently in use.

select * from sys.sysprocesses

returns

Msg 924, Level 14, State 1, Line 1
Database 'G_MAIN_DE' is already open and can only have one user at a time.

and I do not know how to identify the session I have to kill.

An attempt to set it offline

ALTER DATABASE G_MAIN_DE SET OFFLINE WITH ROLLBACK IMMEDIATE

yields

Msg 5064, Level 16, State 1, Line 1
Changes to the state or options of database 'G_MAIN_DE' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
12

To identify the session currently using the database you can try

SELECT request_session_id
FROM   sys.dm_tran_locks
WHERE  resource_database_id = DB_ID('G_MAIN_DE') 
  • 2
    Thank you, thank you, thank you. Finally there is peace in our land. – earthling42 Jan 13 '16 at 15:48
6

This built-in stored procedure shows all connections to a database

exec sp_who2

And this query kills a connection to the database, for example connection SPID #53

exec kill 53
  • Note: The command is just kill 53, there's no exec at the beginning. – vaindil May 4 '18 at 21:05
2

The easiest thing to so is use SSMS Activity Monitor (Right-click server/Activity Monitor/Processes). Sort by Database. Kill whatever process has a hold onthe DB. You might also want to issue the kill command right before you set the DB to single-user and do the drop.

-2

if above process is not working,then go to services and kill the sqlservices in taskmanger. Then will be able to set to multiuser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.