DROP IF EXISTS statement in SQL Server 2016

Microsoft is taking steps to minimize the amount of code that has to be written to get a job done. With that said, they finally introduced the DROP IF EXISTS statement. Previously, you had to write two lines of code to drop an object. The DROP IF EXISTS statement eliminates that.

Step 1 - Example of the OLD WAY

USE [MASTER]
GO
IF OBJECT_ID('dbo.tbl_table1', 'U') IS NOT NULL
 DROP TABLE dbo.tbl_table1;
GO
					

Step 2 - Example of the New Way

USE [MASTER]
GO
DROP TABLE IF EXISTS dbo.tbl_table1
					

Objects that support DROP IF EXISTS

  • AGREGATE
  • ASSEMBLY
  • VIEW
  • DATABASE
  • DEFAULT
  • FUNCTION
  • INDEX
  • PROCEDURE
  • ROLE
  • RULE
  • SCHEMA
  • SEQUENCE
  • SYNONYM
  • TABLE
  • TRIGGER
  • TYPE
  • USER