Click here to Skip to main content
15,884,298 members
Articles / Database Development / SQL Server
Tip/Trick

DROP IF EXISTS in SQL Server 2016

Rate me:
Please Sign up or sign in to vote.
4.02/5 (12 votes)
3 Nov 2015CPOL 15.7K   7   2
SQL Server 2016 introduces the new DROP IF EXISTS syntax

New Statements in SQL Server 2016

One of the most annoying things in SQL Server is checking whether an object exists before it is dropped:

SQL
IF OBJECT_ID('dbo.Product', 'U') IS NOT NULL
 DROP TABLE dbo.Product;
 
IF EXISTS (SELECT * FROM sys.triggers WHERE name = 'trProductInsert')
 DROP TRIGGER trProductInsert

SQL Server 2016 CTP3 adds new DROP IF EXIST (or DIE) syntax:

SQL
DROP FUNCTION IF EXISTS fnCount
DROP PROCEDURE IF EXISTS spReport
DROP TABLE IF EXISTS myTable

In SQL Server 2016 CTP3, the following objects can DIE:

SQL
AGGREGATE      PROCEDURE     TABLE
ASSEMBLY       ROLE          TRIGGER
VIEW           RULE          TYPE
DATABASE       SCHEMA        USER
DEFAULT        SECURITY      POLICY
VIEW           FUNCTION      SEQUENCE
COLUMN         INDEX         SYNONYM
CONSTRAINT

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager Microsoft
Serbia Serbia
Graduated from Faculty of Electrical Engineering, Department of Computer Techniques and Informatics, University of Belgrade, Serbia.
Currently working in Microsoft as Program Manager on SQL Server product.
Member of JQuery community - created few popular plugins (four popular JQuery DataTables add-ins and loadJSON template engine).
Interests: Web and databases, Software engineering process(estimation and standardization), mobile and business intelligence platforms.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Suvendu Shekhar Giri5-Nov-15 9:41
professionalSuvendu Shekhar Giri5-Nov-15 9:41 
QuestionSure would be nice if they would add something like DIE(@ObjectType, @ObjectName, @ReturnValue) Pin
YSLGuru3-Nov-15 4:51
YSLGuru3-Nov-15 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.