Skip to content

Commit a155485

Browse files
authored
Merge pull request #6540 from greglow-sdu/patch-73
Added a more reliable drop database if exists option
2 parents 956c884 + 6ec6b68 commit a155485

1 file changed

Lines changed: 188 additions & 169 deletions

File tree

Lines changed: 188 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,188 @@
1-
---
2-
description: "DROP DATABASE (Transact-SQL)"
3-
title: "DROP DATABASE (Transact-SQL)"
4-
ms.custom: ""
5-
ms.date: "02/21/2019"
6-
ms.prod: sql
7-
ms.prod_service: "synapse-analytics, database-engine, pdw, sql-database"
8-
ms.reviewer: ""
9-
ms.technology: t-sql
10-
ms.topic: reference
11-
f1_keywords:
12-
- "DROP DATABASE"
13-
- "DROP_DATABASE_TSQL"
14-
dev_langs:
15-
- "TSQL"
16-
helpviewer_keywords:
17-
- "snapshots [SQL Server database snapshots], deleting"
18-
- "removing databases"
19-
- "database snapshots [SQL Server], removing"
20-
- "deleting databases"
21-
- "dropping databases"
22-
- "databases [SQL Server], dropping"
23-
- "DROP DATABASE statement"
24-
- "database removal [SQL Server], DROP DATABASE statement"
25-
author: WilliamDAssafMSFT
26-
ms.author: wiassaf
27-
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
28-
---
29-
# DROP DATABASE (Transact-SQL)
30-
31-
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
32-
33-
Removes one or more user databases or database snapshots from an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
34-
35-
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
36-
37-
## Syntax
38-
39-
```syntaxsql
40-
-- SQL Server Syntax
41-
DROP DATABASE [ IF EXISTS ] { database_name | database_snapshot_name } [ ,...n ] [;]
42-
```
43-
44-
```syntaxsql
45-
-- Azure SQL Database, Azure Synapse Analytics and Analytics Platform System Syntax
46-
DROP DATABASE database_name [;]
47-
```
48-
49-
[!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)]
50-
51-
## Arguments
52-
53-
*IF EXISTS*
54-
**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] ( [!INCLUDE[sssql16-md](../../includes/sssql16-md.md)] through [current version](/troubleshoot/sql/general/determine-version-edition-update-level)).
55-
56-
Conditionally drops the database only if it already exists.
57-
58-
*database_name*
59-
Specifies the name of the database to be removed. To display a list of databases, use the [sys.databases](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md) catalog view.
60-
61-
*database_snapshot_name*
62-
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later.
63-
64-
Specifies the name of a database snapshot to be removed.
65-
66-
## General Remarks
67-
68-
A database can be dropped regardless of its state: offline, read-only, suspect, and so on. To display the current state of a database, use the **sys.databases** catalog view.
69-
70-
A dropped database can be re-created only by restoring a backup. Database snapshots cannot be backed up and, therefore, cannot be restored.
71-
72-
When a database is dropped, the [master database](../../relational-databases/databases/master-database.md) should be backed up.
73-
74-
Dropping a database deletes the database from an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system, use [sp_detach_db](../../relational-databases/system-stored-procedures/sp-detach-db-transact-sql.md).
75-
76-
> [!WARNING]
77-
> Dropping a database that has FILE_SNAPSHOT backups associated with it will succeed, but the database files that have associated snapshots will not be deleted to avoid invalidating the backups referring to these database files. The file will be truncated, but will not be physically deleted in order to keep the FILE_SNAPSHOT backups intact. For more information, see [SQL Server Backup and Restore with Microsoft Azure Blob Storage Service](../../relational-databases/backup-restore/sql-server-backup-and-restore-with-microsoft-azure-blob-storage-service.md). **Applies to**: [!INCLUDE[sssql16-md](../../includes/sssql16-md.md)] through [current version](/troubleshoot/sql/general/determine-version-edition-update-level).
78-
79-
### [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]
80-
81-
Dropping a database snapshot deletes the database snapshot from an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] and deletes the physical NTFS File System sparse files used by the snapshot. For information about using sparse files by database snapshots, see [Database Snapshots](../../relational-databases/databases/database-snapshots-sql-server.md). Dropping a database snapshot clears the plan cache for the instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. Clearing the plan cache causes a recompilation of all subsequent execution plans and can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] error log contains the following informational message: " [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] has encountered %d occurrence(s) of cachestore flush for the '%s' cachestore (part of plan cache) due to some database maintenance or reconfigure operations". This message is logged every five minutes as long as the cache is flushed within that time interval.
82-
83-
## Interoperability
84-
85-
### SQL Server
86-
87-
To drop a database published for transactional replication, or published or subscribed to merge replication, you must first remove replication from the database. If a database is damaged or replication cannot first be removed or both, in most cases you still can drop the database by using ALTER DATABASE to set the database offline and then dropping it.
88-
89-
If the database is involved in log shipping, remove log shipping before dropping the database. For more information, see [About Log Shipping](../../database-engine/log-shipping/about-log-shipping-sql-server.md).
90-
91-
## Limitations and Restrictions
92-
93-
[System databases](../../relational-databases/databases/system-databases.md) cannot be dropped.
94-
95-
The DROP DATABASE statement must run in autocommit mode and is not allowed in an explicit or implicit transaction. Autocommit mode is the default transaction management mode.
96-
97-
You cannot drop a database currently being used. This means open for reading or writing by any user. One way to remove users from the database is to use ALTER DATABASE to set the database to SINGLE_USER.
98-
99-
> [!WARNING]
100-
> This is not a fail-proof approach, since first consecutive connection made by any thread will receive the SINGLE_USER thread, causing your connection to fail. SQL Server does not provide a built-in way to drop databases under load.
101-
102-
### [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]
103-
104-
Any database snapshots on a database must be dropped before the database can be dropped.
105-
106-
Dropping a database enable for Stretch Database does not remove the remote data. If you want to delete the remote data, you have to remove it manually.
107-
108-
### [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)]
109-
110-
You must be connected to the master database to drop a database.
111-
112-
The DROP DATABASE statement must be the only statement in a SQL batch and you can drop only one database at a time.
113-
114-
### [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)]
115-
116-
You must be connected to the master database to drop a database.
117-
118-
The DROP DATABASE statement must be the only statement in a SQL batch and you can drop only one database at a time.
119-
120-
## Permissions
121-
122-
### [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]
123-
124-
Requires the **CONTROL** permission on the database, or **ALTER ANY DATABASE** permission, or membership in the **db_owner** fixed database role.
125-
126-
### [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)]
127-
128-
Only the server-level principal login (created by the provisioning process) or members of the **dbmanager** database role can drop a database.
129-
130-
### [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
131-
132-
Requires the **CONTROL** permission on the database, or **ALTER ANY DATABASE** permission, or membership in the **db_owner** fixed database role.
133-
134-
## Examples
135-
136-
### A. Dropping a single database
137-
138-
The following example removes the `Sales` database.
139-
140-
```sql
141-
DROP DATABASE Sales;
142-
```
143-
144-
### B. Dropping multiple databases
145-
146-
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later.
147-
148-
The following example removes each of the listed databases.
149-
150-
```sql
151-
DROP DATABASE Sales, NewSales;
152-
```
153-
154-
### C. Dropping a database snapshot
155-
156-
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later.
157-
158-
The following example removes a database snapshot, named `sales_snapshot0600`, without affecting the source database.
159-
160-
```sql
161-
DROP DATABASE sales_snapshot0600;
162-
```
163-
164-
## See Also
165-
166-
- [ALTER DATABASE](../../t-sql/statements/alter-database-transact-sql.md)
167-
- [CREATE DATABASE](../../t-sql/statements/create-database-transact-sql.md)
168-
- [EVENTDATA](../../t-sql/functions/eventdata-transact-sql.md)
169-
- [sys.databases](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)
1+
---
2+
description: "DROP DATABASE (Transact-SQL)"
3+
title: "DROP DATABASE (Transact-SQL)"
4+
ms.custom: ""
5+
ms.date: "06/25/2021"
6+
ms.prod: sql
7+
ms.prod_service: "synapse-analytics, database-engine, pdw, sql-database"
8+
ms.reviewer: ""
9+
ms.technology: t-sql
10+
ms.topic: reference
11+
f1_keywords:
12+
- "DROP DATABASE"
13+
- "DROP_DATABASE_TSQL"
14+
dev_langs:
15+
- "TSQL"
16+
helpviewer_keywords:
17+
- "snapshots [SQL Server database snapshots], deleting"
18+
- "removing databases"
19+
- "database snapshots [SQL Server], removing"
20+
- "deleting databases"
21+
- "dropping databases"
22+
- "databases [SQL Server], dropping"
23+
- "DROP DATABASE statement"
24+
- "database removal [SQL Server], DROP DATABASE statement"
25+
author: WilliamDAssafMSFT
26+
ms.author: wiassaf
27+
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
28+
---
29+
# DROP DATABASE (Transact-SQL)
30+
31+
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
32+
33+
Removes one or more user databases or database snapshots from an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
34+
35+
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
36+
37+
## Syntax
38+
39+
```syntaxsql
40+
-- SQL Server Syntax
41+
DROP DATABASE [ IF EXISTS ] { database_name | database_snapshot_name } [ ,...n ] [;]
42+
```
43+
44+
```syntaxsql
45+
-- Azure SQL Database, Azure Synapse Analytics and Analytics Platform System Syntax
46+
DROP DATABASE database_name [;]
47+
```
48+
49+
[!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)]
50+
51+
## Arguments
52+
53+
*IF EXISTS*
54+
**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] ( [!INCLUDE[sssql16-md](../../includes/sssql16-md.md)] through [current version](/troubleshoot/sql/general/determine-version-edition-update-level)).
55+
56+
Conditionally drops the database only if it already exists.
57+
58+
*database_name*
59+
Specifies the name of the database to be removed. To display a list of databases, use the [sys.databases](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md) catalog view.
60+
61+
*database_snapshot_name*
62+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later.
63+
64+
Specifies the name of a database snapshot to be removed.
65+
66+
## General Remarks
67+
68+
A database can be dropped regardless of its state: offline, read-only, suspect, and so on. To display the current state of a database, use the **sys.databases** catalog view.
69+
70+
A dropped database can be re-created only by restoring a backup. Database snapshots cannot be backed up and, therefore, cannot be restored.
71+
72+
When a database is dropped, the [master database](../../relational-databases/databases/master-database.md) should be backed up.
73+
74+
Dropping a database deletes the database from an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system, use [sp_detach_db](../../relational-databases/system-stored-procedures/sp-detach-db-transact-sql.md).
75+
76+
> [!WARNING]
77+
> Dropping a database that has FILE_SNAPSHOT backups associated with it will succeed, but the database files that have associated snapshots will not be deleted to avoid invalidating the backups referring to these database files. The file will be truncated, but will not be physically deleted in order to keep the FILE_SNAPSHOT backups intact. For more information, see [SQL Server Backup and Restore with Microsoft Azure Blob Storage Service](../../relational-databases/backup-restore/sql-server-backup-and-restore-with-microsoft-azure-blob-storage-service.md). **Applies to**: [!INCLUDE[sssql16-md](../../includes/sssql16-md.md)] through [current version](/troubleshoot/sql/general/determine-version-edition-update-level).
78+
79+
### [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]
80+
81+
Dropping a database snapshot deletes the database snapshot from an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] and deletes the physical NTFS File System sparse files used by the snapshot. For information about using sparse files by database snapshots, see [Database Snapshots](../../relational-databases/databases/database-snapshots-sql-server.md). Dropping a database snapshot clears the plan cache for the instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. Clearing the plan cache causes a recompilation of all subsequent execution plans and can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] error log contains the following informational message: " [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] has encountered %d occurrence(s) of cachestore flush for the '%s' cachestore (part of plan cache) due to some database maintenance or reconfigure operations". This message is logged every five minutes as long as the cache is flushed within that time interval.
82+
83+
## Interoperability
84+
85+
### SQL Server
86+
87+
To drop a database published for transactional replication, or published or subscribed to merge replication, you must first remove replication from the database. If a database is damaged or replication cannot first be removed or both, in most cases you still can drop the database by using ALTER DATABASE to set the database offline and then dropping it.
88+
89+
If the database is involved in log shipping, remove log shipping before dropping the database. For more information, see [About Log Shipping](../../database-engine/log-shipping/about-log-shipping-sql-server.md).
90+
91+
## Limitations and Restrictions
92+
93+
[System databases](../../relational-databases/databases/system-databases.md) cannot be dropped.
94+
95+
The DROP DATABASE statement must run in autocommit mode and is not allowed in an explicit or implicit transaction. Autocommit mode is the default transaction management mode.
96+
97+
> [!WARNING]
98+
> You cannot drop a database currently being used. This means locks being held for reading or writing by any user. One way to remove users from the database is to use ALTER DATABASE to set the database to SINGLE_USER. In this strategy, you should execute the ALTER DATABASE and DROP DATABASE in the same batch, to avoid another connection claiming single user session allowed. See [Example D below](#dropping-a-database-after-checking-if-it-exists).
99+
100+
### [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]
101+
102+
Any database snapshots on a database must be dropped before the database can be dropped.
103+
104+
Dropping a database enable for Stretch Database does not remove the remote data. If you want to delete the remote data, you have to remove it manually.
105+
106+
### [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)]
107+
108+
You must be connected to the master database to drop a database.
109+
110+
The DROP DATABASE statement must be the only statement in a SQL batch and you can drop only one database at a time.
111+
112+
### [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)]
113+
114+
You must be connected to the master database to drop a database.
115+
116+
The DROP DATABASE statement must be the only statement in a SQL batch and you can drop only one database at a time.
117+
118+
## Permissions
119+
120+
### [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]
121+
122+
Requires the **CONTROL** permission on the database, or **ALTER ANY DATABASE** permission, or membership in the **db_owner** fixed database role.
123+
124+
### [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)]
125+
126+
Only the server-level principal login (created by the provisioning process) or members of the **dbmanager** database role can drop a database.
127+
128+
### [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
129+
130+
Requires the **CONTROL** permission on the database, or **ALTER ANY DATABASE** permission, or membership in the **db_owner** fixed database role.
131+
132+
## Examples
133+
134+
### A. Dropping a single database
135+
136+
The following example removes the `Sales` database.
137+
138+
```sql
139+
DROP DATABASE Sales;
140+
```
141+
142+
### B. Dropping multiple databases
143+
144+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later.
145+
146+
The following example removes each of the listed databases.
147+
148+
```sql
149+
DROP DATABASE Sales, NewSales;
150+
```
151+
152+
### C. Dropping a database snapshot
153+
154+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later.
155+
156+
The following example removes a database snapshot, named `sales_snapshot0600`, without affecting the source database.
157+
158+
```sql
159+
DROP DATABASE sales_snapshot0600;
160+
```
161+
162+
### D. Dropping a database after checking if it exists
163+
164+
165+
The following example first checks to see if a database named `Sales` exists. If so, the example changes the database named `Sales` to single-user mode to force disconnect of all other sessions, then drops the database. For more information on SINGLE_USER, see [ALTER DATABASE SET options](alter-database-transact-sql-set-options.md).
166+
167+
168+
```sql
169+
USE tempdb;
170+
GO
171+
DECLARE @SQL nvarchar(1000);
172+
IF EXISTS (SELECT 1 FROM sys.databases WHERE [name] = N'Sales')
173+
BEGIN
174+
SET @SQL = N'USE [Sales];
175+
176+
ALTER DATABASE Sales SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
177+
USE [tempdb];
178+
179+
DROP DATABASE Sales;';
180+
EXEC (@SQL);
181+
END;
182+
```
183+
## See Also
184+
185+
- [ALTER DATABASE](../../t-sql/statements/alter-database-transact-sql.md)
186+
- [CREATE DATABASE](../../t-sql/statements/create-database-transact-sql.md)
187+
- [EVENTDATA](../../t-sql/functions/eventdata-transact-sql.md)
188+
- [sys.databases](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)

0 commit comments

Comments
 (0)