All Uses of DROP IF EXISTS in SQL Server

Hello everyone. In this article, I will give information about all DROP IF EXISTS usages in SQL Server.

In SQL Server you may need to use DROP IF EXISTS in some cases.

You can easily do this using the code below.

Note: The commands below work on SQL Server 2016 and above.

1 -) Drop a Table

DROP TABLE IF EXISTS dbo.TableName;

2 -) Drop a View

DROP VIEW IF EXISTS ViewName;

3 -) Drop a Index

DROP INDEX IF EXISTS my_index ON IndexName;

4 -) Drop a Procedure

DROP PROCEDURE IF EXISTS ProcedureName;

5 -) Drop a Function

DROP FUNCTION IF EXISTS FunctionName;

6 -) Drop a Schema

DROP SCHEMA IF EXISTS SchemaName;

7 -) Drop a Trigger

DROP TRIGGER IF EXISTS my_trigger ON TriggerName

8 -) Drop a Foreign Key

ALTER TABLE TabloName
DROP CONSTRAINT IF EXISTS ForeignKeyName;

9 -) Drop a User

DROP USER IF EXISTS UserName;

The above examples show different usage scenarios of “DROP IF EXISTS” command in SQL Server. These commands are useful for checking objects for existence before deleting them and reduce the chance of errors when writing code.

Good luck to everyone in business and life.

31 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!