In this SQL Tutorial I will present sql server check if table exists code and give example.
To check if table exists I will use SQL Server OBJECT_ID function and provide table name with schema name to avoid issues in the future.
The code below checks if "MyTableName" exists and if doesn't IS NOT NULL drops the table and recreates the table
IF OBJECT_ID('dbo.MyTableName') IS NOT NULL -- this statement check if given table name exists (is not null = exists)
DROP TABLE dbo.MyTableName -- if yes then drop
GO
CREATE TABLE dbo.MyTableName (ID INT)
NOTE: DROP TABLE permanently removes the data so ensure this is what you want to do.
I hope this example will help you you tsql queries where you want to check if tables exists and drop the table.
Take care
Emil