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.
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly