HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to drop SQL Server database currently in use and in Single user mode

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlusermodeanddatabasedropsinglehowserveruse

Problem

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.

Solution

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

Code Snippets

exec sp_who2
exec kill 53

Context

StackExchange Database Administrators Q#49385, answer score: 10

Revisions (0)

No revisions yet.