Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot drop a database in Azure Data Studio, because it's currently in use

I cannot drop custom databases in Azure Data Studio, because they are currently in use.

I've been looking for various ways to close the zap database, but I cannot find any in the UI.

Only by restarting Azure Data Studio, is the zap database in an "Auto Closed" state, which lets me drop it using:

drop database zap;

How do I close a connection to a database without restarting Azure Data Studio?

Open:

Auto-closed:

like image 447
Shuzheng Avatar asked Jan 02 '20 12:01

Shuzheng


People also ask

Can't drop existing database SQL?

If you want to delete the database, you will get this error if there is an open session on the database. First, set the database to single_user mode. Then you can delete it.

Can't drop database because it currently in use in MS SQL Server?

This article contains the Fix/Solution for an error 'Cannot drop database because it is currently in use in MS SQL Server' This error occurs when we try Delete or Drop database while the database connection is used by other users or other resources. So we need to close existing connections first then we need to Drop or Delete the database.

What happens when you drop a database in azure Databricks?

If the database does not exist, an exception is thrown. If the database to drop does not exist, nothing happens. Dropping a non-empty database triggers an exception. Enabled by default. Dropping a non-empty database also drops all associated tables and functions. Learn how to use the CREATE VIEW syntax of the SQL language in Azure Databricks.

Why am I getting database connection error when trying to delete/drop?

This error occurs when we try Delete or Drop database while the database connection is used by other users or other resources. So we need to close existing connections first then we need to Drop or Delete the database.

How to drop a database from a single user?

Try to switch to another database and then, to drop it: For SQL server mgmt. studio: Right click database: Properties -> Options -> Restrict Access : Set to "Single User" and perform the drop afterwards. invoke-sqlcmd -ServerInstance localhost 'exec sp_who' | where-object {$_.dbname -eq 'myDbName'} returns nothing. Yet it till complains.


2 Answers

Someone else is connected, so you need to set it to SINGLE_USER. This will terminate any existing connections, so that the database can be dropped.

USE master;
GO

ALTER DATABASE zap SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE zap;
GO
like image 197
Larnu Avatar answered Sep 24 '22 06:09

Larnu


You can try the following:

use master;
go
ALTER DATABASE zap SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Go

then drop database.

There is no need to find session which is connected to your database. it can be as simple as an object explorer open in another window. kill the session with the command above and then drop the database.

https://docs.microsoft.com/en-us/sql/relational-databases/databases/set-a-database-to-single-user-mode?view=sql-server-ver15

like image 33
Gauravsa Avatar answered Sep 22 '22 06:09

Gauravsa