Simplified drop statement in SQL Server 2016

Krishna KV
 
Team Leader, Aspire Systems
July 8, 2016
 
Rate this article
 
Views
8534

SQL Server 2016 makes the drop and if exists into a single statement. It will check for the object existence. If the object exists, it will execute the drop statement. Else it will continue with the next statement.

Before SQL Server 2016

 IF OBJECT_ID('dbo.test', 'U') IS NOT NULL
  DROP TABLE dbo.test;	
 		 
 		      (or)
 
 IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES  WHERE TABLE_NAME = 'Test')
 DROP TABLE dbo.Test
 

This statement can be written in SQL Server 2016 as:

 CREATE TABLE test(id INT,name VARCHAR(200),rollno INT  CONSTRAINT unq UNIQUE)
 GO
 
 CREATE PROCEDURE usp_test AS 
 BEGIN
 SELECT * FROM test
 END

To delete a column

 ALTER TABLE dbo.test DROP COLUMN  IF EXISTS rollno

To delete a constraint

 ALTER TABLE dbo.test DROP CONSTRAINT IF EXISTS unq

To delete a table

 DROP TABLE IF EXISTS  dbo.test

To delete a procedure

 DROP PROCEDURE IF EXISTS dbo.usp_test
Category : SQL

Author Info

Krishna KV
 
Team Leader, Aspire Systems
 
Rate this article
 
Krishna K.V has been working in IT Industry for over 7+ years. He holds a Master Degree in Information Technology. He is more interested to learn and share new technologies, ...read more
 

Leave a comment