DROP Database SQL Server
Let’s learn how to Drop Database in SQL Server when the users are connected to the SQL Server Database.
You may find the need to close all the existing database connections in a database before restoring the database, before detaching the database and for this, you need to get the database in SINGLE_USER mode.
If you ever try to drop a database when users are connected to the SQL Server Database then you will receive the below mentioned error message.
Drop failed for Database 'MyTechMantra'. (Microsoft.SqlServer.Smo) Cannot drop database "" because it is currently in use. (Microsoft SQL Server, Error: 3702) Cannot drop database because it is currently in use Microsoft SQL Server Error 3702
The reason why you end up getting the above-mentioned error is when SQL Server is Unable to Get Exclusive Access to SQL Server Database.
Warning: Be careful before executing Drop Database TSQL Command (More commonly used terminology is to Database Drop). Follow the steps mentioned in this article & learn How to Delete Database in SQL Server.
Most commonly, to drop a database, it is referred to as “sql drop db“, “drop db” or “dropdatabase“. However, the correct syntax to Drop Database in SQL Server is DROP DATABASE
However, DROP DATABASE Command will fail when other users are already connected to the database. Check the example mentioned below in the article to understand How to Drop a Database by Killing Existing Connections.
Different Ways to Get Exclusive Access to Drop Database in SQL Server (SQL Server DROP Database)
- Drop Database SQL Server Using SQL Server Management Studio (SSMS)
- Drop Database SQL Server Using TSQL Query
Drop Database in SQL Server Using SQL Server Management Studio
- Connect to SQL Server Management Studio; expand Database Node -> Right click the Databases which you want to Drop -> Select Delete from the drop-down menu to open up Delete Object dialog box as shown in the snippet below.
2. Select the Check box “Close existing connections” to Drop Existing Connections before Dropping the SQL Server Database and click OK to Drop Database in SQL Server.
3. By selecting “Delete backup and restore history information for databases” option you will be able to remove the database backup and restore history which is stored in MSDB system database.
Drop Database SQL Server Using TSQL Query
Execute the below TSQL code to Drop Database in SQL Server Using TSQL Query.
/* Delete Database Backup and Restore History from MSDB System Database */ EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'MyTechMantra' GO /* Query to Get Exclusive Access of SQL Server Database before Dropping the Database */ USE [master] GO ALTER DATABASE [MyTechMantra] SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO /* Query to Drop Database in SQL Server */ DROP DATABASE [MyTechMantra] GO
In the article, you have seen different ways by which you can in SQL Server Drop Database by getting exclusive access to SQL Server Database.
Trending SQL Server Tips
- SQL Delete Duplicate Rows from a SQL Table in SQL Server
- How to Configure an SPN for SQL Server Site Database Servers
- Register a Service Principal Name for Kerberos Connections
- How to troubleshoot the “Cannot generate SSPI context” error message
- How to use Kerberos authentication in SQL Server
- Download Microsoft Kerberos Configuration Manager for SQL Server – A diagnostic tool that helps troubleshoot Kerberos related connectivity issues with SQL Server and SQL Server Reporting Services