sql server drop table if exists – How to drop a table if it exists?

sql server drop table if exists – use IF EXISTS clause to remove the table only if it exists. Checking whether a database objects like table, stored procedure, function, etc.

sql server drop table if exists

SQL – DROP or DELETE Table, The SQL DROP TABLE statement is used to remove a table definition.

DROP TABLE [IF EXISTS]  [database_name.][schema_name.]table_name;

SQL Server DROP TABLE IF EXISTS Examples

Drop a table that does not exist

DROP TABLE IF EXISTS deals.revenues;

SQL Server DROP TABLE

The following statement creates a new table named dispatch in the deals schema:
Drop a single table example

CREATE TABLE deals.dispatch (
    dispatch_id INT PRIMARY KEY,
    dispatch_note VARCHAR (255) NOT NULL,
    dispatch_date DATE NOT NULL
);

To remove the dispatch table, you use the following statement:

DROP TABLE deals.dispatch;

Drop a table with a foreign key constraint example

The following statement creates two new tables named manager_networks and managers in the accession schema:

CREATE SCHEMA accession;
GO

CREATE TABLE accession.manager_networks (
    network_id INT IDENTITY PRIMARY KEY,
    network_name VARCHAR (50) NOT NULL
);

CREATE TABLE accession.managers (
    manager_id INT IDENTITY PRIMARY KEY,
    manager_name VARCHAR (50) NOT NULL,
    network_id INT NOT NULL,
    FOREIGN KEY (network_id) REFERENCES accession.manager_networks (network_id)
);

Let’s try to drop the manager_networks table:

DROP TABLE accession.manager_networks;

don’t Miss : Sql Check If Table Exists

DROP TABLE accession.manager_networks;
DROP TABLE accession.managers;
DROP TABLE accession.managers, accession.manager_networks;

I hope you get an idea about sql server drop table if exists.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment