Msg 3702, Level 16, State 3, Line 2 Cannot drop database “DataBaseName” because it is currently in use.

Try to drop a SQL database, but get following errors:

USE Master;
GO
DROP DATABASE dbTEST;
GO
Msg 3702, Level 16, State 4, Line 1
Cannot drop database "dbTEST" because it is currently in use.

SOLUTION

Kill the blocking sessions, or set database in SINGER_USER mode with “Rollback Immediate” option to kill  and rollback the transactions.

USE [master]
GO
ALTER DATABASE dbTEST SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
USE [master]
GO
DROP DATABASE dbTEST;
GO

After this, check both data files and log files are cleaned up as well.

Advertisement

How to Set SQL or Oracle Database to Single-User Mode and How to Check Single User or Multi User Mode

In Oracle database, place the instance in restricted mode.  The users already connected to the database will not get disconnected. You need to manually kill these sessions.

SQL> alter system enable restricted session;

OR

Startup in restricted mode.

SQL> startup restrict;

Check the instance mode

SQL> select logins from v$instance;

LOGINS
----------
RESTRICTED

In SQL database,  Below are the two most popular ways to set up database to single-user mode in SQL Server:

Using SQL Server Management Studio

Right-click the database to change, and then click Properties -> click the Options page->From the Restrict Access option, select Single.
If other users are connected to the database, an Open Connections message will appear. To change the property and close all other connections, click Yes.

Using Transact-SQL

USE master;
GO
ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO

Check SQL Database Single User or Multi User Mode

a)  Select ServerProperty(‘IsSingleUser’)

It should return 1, 0, or Null.

1 = Single user.
0 = Not single user
NULL = Input is not valid, or an error.

use MyDatabase
go
Select ServerProperty('IsSingleUser')

b) Select from sys.database,  it should retuen:

  • SINGLE_USER
  • MULTI_USER
SELECT user_access_desc 
FROM sys.databases 
WHERE name = 'MyDatabase'

3) From SSMS, Right-click the database , and then click Properties -> click the Options page->From the Restrict Access option.