Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

DROP IF EXISTS in SQL Server 2016

4.02/5 (12 votes)
3 Nov 2015CPOL 16.1K  
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)