Scenario:
You are working as Database developer in Health Insurance company, You need to generate script to drop database that can be used in different environments such as QA, UAT and Production.
Solution:
There are multiple ways to drop the database in SQL Server. You can use GUI part of SSMS to drop database.
By using GUI:
Right Click on the database that you would like to drop and then hit Delete as shown below.
How to Delete Database in SQL Server - SQL Server Tutorial
In below windows, check the checkbox Close existing connections. This will close any existing connections, if you don't check this, the drop might fail if there are open connections to database.
How to drop Database in SQL Server - SQL Server / TSQL Tutorial Step by Step
Use TSQL To Drop Database in SQL Server:
At this point you can hit Script button in above window to generate script so you can use anytime you like. Once you hit Script and open in new window. Below Script will be create.
USE [master]
GO
ALTER DATABASE [TechBrothersIT]
SET SINGLE_USER
WITH
ROLLBACK IMMEDIATE
GO
USE [master]
GO
DROP DATABASE [TechBrothersIT]
GO
The first part of script will set the database to Single User mode and rollback any open transactions immediately. Second Part of script will drop the database.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.