Drop Database in SQL Server

Updated: Jan 5

Let's learn how to Drop SQL databases in SQL Server when you have existing connections for the SQL database.


Suppose you require to drop the [ImportDB] database in the SQL instance. For this purpose, we use the DROP DATABASE statement as below.


Drop database ImportDB

The below DROP DATABASE script shows that it cannot drop the database because it is currently in use. You get the message because SQL Server cannot get exclusive access to the SQL Server database.


Drop Database in SQL Server

We can use the ALTER DATABASE…SET SINGLE_USER WITH ROLLBACK IMMEDIATE to get exclusive access to SQL Database to drop it.


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

The script can drop the database as shown below.


Drop database when database is in use

7 views0 comments