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
Leave a comment