When writing T-SQL code, we often write code to check if the database object exists first and then take some action. Is there an easier way to do this in SQL Server 2016?
SolutionMicrosoft SQL Server 2016 was released to manufacturing on June 1st. Please see this SQL Server Team BLOG for the full details.
This product release contains many new features in the database engine. One new feature is the DROP IF EXISTS syntax for use with Data Definition Language (DDL) statements. Twenty existing T-SQL statements have this new syntax added as an optional clause. Please see the "What's New in Database Engine" article for full details. In this tip we will use examples of how this new feature can be used.
Business Problem for ExampleA local business owner has asked you for help to keep track of his inventory. His business is centered on buying, assembling and customizing model cars for wealthy clients. Did you know that the toy industry has an estimated annual sales of 25 billion dollars with toy models taking a 1.5 billion dollar share?
Prior KnowledgeThis article assumes you know how to create a Azure SQL Server and Azure SQL Database. You should be familiar with writing Transact SQL (T-SQL) in the SQL Server Management Studio (SSMS) since we will be leveraging this interactive development environment (IDE) to craft the business solution.
Please make sure you are using the latest version of SSMS. This product was separated from the database installation as a different product. This separation allows the development team to release updates on the IDE on a monthly schedule versus updates on the database on a yearly schedule. The July 2016 release contains many new features.
Azure SQL Server ObjectsWe will be reusing the Azure SQL Server that I created last time. The server name is mssqltips2016 , the administrator login name is jminer , and the firewall rule name is MyLaptop . We will be creating a new database named AUTOS to contain the database objects for our business solution.
If you do not have these objects in your Azure Portal, please follow the steps my previousarticle to get to this point.

Article Syllabus
I will be using the model car business as a case study for showing coding examples for 15 out of the 20 data definition language statements impacted by the new syntax. The last 5 statements are not used in common practice by most developers. Each statement will be showcased with restart able T-SQL code that uses the algorithm below.
Test existence of database object. Drop database object if it exists. Create new database object.The new DROP IF EXISTS syntax replaces the old block of code that used system catalog views to determine the existence of an object. Basically, the new syntax combines steps one and two into a smaller set of code to produce the same results.
For each statement, I give an example of both the old and new way to accomplish the same task. If the CREATE or DROP statements are executed on existing or missing objects respectively, errors will be generated. I show the output of such negative test cases. Last but not least, hyperlinks to each statement are given for the reader to look up detailed information if they desire.
SQL Server Drop Database If Exists A database object is the main container in which other user defined objects are stored within. This definition becomes crystal clear when you use Azure SQL Database. Let's get this article started by creating a database named [AUTOS] for our business solution. Make sure you are in a session connected to the [master] database when you execute the T-SQL code below. /*Create autos database
*/
-- Old block of code
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'AUTOS')
DROP DATABASE [AUTOS]
GO
-- New block of code
DROP DATABASE IF EXISTS [AUTOS]
GO
-- Add new database
CREATE DATABASE [AUTOS]
(
MAXSIZE = 2GB,
EDITION = 'STANDARD',
SERVICE_OBJECTIVE = 'S0'
)
GO
One might ask what happens when you try to execute DROP DATABASE statement on non-existing database? The following error message is generated.

Or, one might ask what happens when you try to execute CREATE DATABASE statement on existing database? The following error message is generated.

SQL Server Drop Schema If Exists I personally think that the schema object is underutilized in database design. This object allows the designer to secure and/or hide groups of objects at a higher level. If you do not use custom schema objects, the default schema [dbo] does exist. However, you are probably granting privileges at the lower level such as a table or view. In our business solution, we want to create a [TOY] schema for active data and/or reference tables; and an [AUDIT] schema to trace events leading up to the current database state. Going forward, make sure you execute any code in session connected to the [AUTOS] database.
The T-SQL code below creates the required schemas.
/*Create toy schema
*/
-- Old block of code
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'TOY')
DROP SCHEMA [TOY]
GO
-- New block of code
DROP SCHEMA IF EXISTS [TOY]
GO
-- Add new schema.
CREATE SCHEMA [TOY] AUTHORIZATION [dbo]
GO
/*
Create audit schema
*/
-- Old block of code
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'AUDIT')
DROP SCHEMA [AUDIT]
GO
-- New block of code
DROP SCHEMA IF EXISTS [AUDIT]
GO
-- Add new schema.
CREATE SCHEMA [AUDIT] AUTHORIZATION [dbo]
GO
One might ask what happens when you try to execute DROP SCHEMA statement with non-existing schema? The following error message is generated.

Or, one might ask what happens when you try to execute CREATE SCHEMA statement with an existing schema? The following error message is generated.

SQL Server Drop Table If Exists
A table is the key storage object in any relational database management system ( RDBMS ). We will start building our business solution with one active table, one audit table and two reference tables.