65.9K
CodeProject is changing. Read more.
Home

DROP IF EXISTS in SQL Server 2016

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.02/5 (12 votes)

Nov 3, 2015

CPOL
viewsIcon

16080

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:

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:

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:

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