Check if table exists then delete it in SQL Server


I am checking this article, which allow you to drop all tables of SQL Server database, but I just want to delete a specific table of the database, so how can I do it?

to check if table exists then drop it completely from the database, without risking any other DB table in SQL Server.


Asked by:- pika
0
: 292 At:- 8/13/2021 12:41:50 PM
SQL drop if table exists SQL Server






1 Answers
profileImage Answered by:- vikas_jk

If you are using SQL Server older than 2016, you can use below query to check if table exists and then drop it

IF EXISTS(SELECT *
          FROM   dbo.YourTableName)
  DROP TABLE dbo.YourTableName

The above will drop table if it contains Rows and table exists, otherwise it will throw error.

For Newer version (SQL Server 16+, Version 13.X), you can use below query (Works in SQL Azure database also)

DROP TABLE IF EXISTS dbo.YourTableName

Hope it helps.

1
At:- 8/13/2021 2:25:56 PM
Excellent thanks. 0
By : pika - at :- 8/16/2021 11:15:33 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use