Skip to content

Added a more reliable drop database if exists option#6540

Merged
ktoliver merged 9 commits intoMicrosoftDocs:livefrom
greglow-sdu:patch-73
Jun 28, 2021
Merged

Added a more reliable drop database if exists option#6540
ktoliver merged 9 commits intoMicrosoftDocs:livefrom
greglow-sdu:patch-73

Conversation

@greglow-sdu
Copy link
Copy Markdown
Contributor

We've had a lengthy discussion in the MVP email list about how to reliably drop a database if it exists. Most methods had issues and simply do not work reliably. This is the best method that we've come up with, and it should be recorded in the documentation for others to use.

We've had a lengthy discussion in the MVP email list about how to reliably drop a database if it exists. Most methods had issues and simply do not work reliably. This is the best method that we've come up with, and it should be recorded in the documentation for others to use.
@PRMerger9
Copy link
Copy Markdown
Contributor

@greglow-sdu : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@ktoliver ktoliver added the aq-pr-triaged tracking label for the PR review team label Jun 24, 2021
Copy link
Copy Markdown
Contributor

@WilliamDAssafMSFT WilliamDAssafMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @greglow-sdu for the suggestion for this scenario. I can't help but think that this solution is a bit overcomplicated. I believe it can be simplified. I have suggested edits, please review and incorporate.

You should also increment the ms.date: "06/24/2021"

Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
@greglow-sdu
Copy link
Copy Markdown
Contributor Author

greglow-sdu commented Jun 24, 2021 via email

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

WilliamDAssafMSFT commented Jun 24, 2021 via email

@greglow-sdu
Copy link
Copy Markdown
Contributor Author

greglow-sdu commented Jun 24, 2021 via email

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

Because you were in the master database when you issued the ALTER, you don't know that you are the single user. So, periodically, that would fail too.

@greglow-sdu Do you have a sample script or scenario that reproduces the ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE not claiming a lock on the single-user database, even if called from another database?

For example,

USE tempdb;
ALTER DATABASE dropdb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
SELECT db_name(resource_database_id), request_session_id, request_type, * FROM sys.dm_tran_locks;

You should see that the SESSION has still requested a SHARED DATABASE lock. Trying to USE dropdb from another session fails with Msg 924, Level 14, State 1, Line 1 Database 'dropdb' is already open and can only have one user at a time, and the DROP DATABASE succeeds.

@greglow-sdu
Copy link
Copy Markdown
Contributor Author

greglow-sdu commented Jun 24, 2021 via email

Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
@greglow-sdu
Copy link
Copy Markdown
Contributor Author

greglow-sdu commented Jun 25, 2021 via email

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

@greglow-sdu I suppose it couldn't hurt to have the extra USE lines in there aside from confusion. I don't understand how, even if something else claimed the session after the ALTER DATABASE, how it couldn't then be seized again when the second USE relinquishes the single-user session. We'd need to do a better job of explaining them justified by a mysterious, undocumented edge case. In any case, line 171 still needs to change, I've proposed that. Go ahead and accept my revisions and review yourself.

@greglow-sdu
Copy link
Copy Markdown
Contributor Author

greglow-sdu commented Jun 25, 2021 via email

@PRMerger10
Copy link
Copy Markdown
Contributor

@WilliamDAssafMSFT : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@PRMerger15
Copy link
Copy Markdown
Contributor

@WilliamDAssafMSFT : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@PRMerger9
Copy link
Copy Markdown
Contributor

@WilliamDAssafMSFT : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@PRMerger7
Copy link
Copy Markdown
Contributor

@WilliamDAssafMSFT : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

@greglow-sdu I've approved my suggestion commits, take a look.

@ktoliver
Copy link
Copy Markdown
Contributor

ktoliver commented Jun 25, 2021

FYI, here's a snapshot of the diff (the diff isn't visible in GitHub):

image

We'll wait for a #sign-off to merge.

Comment thread docs/t-sql/statements/drop-database-transact-sql.md Outdated
@PRMerger15
Copy link
Copy Markdown
Contributor

@WilliamDAssafMSFT : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

Thanks @ktoliver, the merge conflict resolution does that sometimes.
I have also updated the ms.date.

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

FYI, here's a snapshot of the diff (the diff isn't visible in GitHub):

image

We'll wait for a #sign-off to merge.

@greglow-sdu What do you think?

@greglow-sdu
Copy link
Copy Markdown
Contributor Author

greglow-sdu commented Jun 28, 2021 via email

@PRMerger6
Copy link
Copy Markdown
Contributor

@greglow-sdu: I'm sorry - only the author of this article, @WilliamDAssafMSFT, can sign off on your changes. But we do have an exception process - if you are on the Microsoft content or product team for this product area, you can ask the PR review team to review and merge it by sending mail to the techdocprs alias.

@WilliamDAssafMSFT
Copy link
Copy Markdown
Contributor

#sign-off

@ktoliver ktoliver merged commit a155485 into MicrosoftDocs:live Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants