Support our 100% FREE Projects: Donate Now OR Sponsor Now


  

sql server check if table exists

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

Share: Share on FacebookShare on Google PlusTweet it
Comments Add Comment
No data was returned. Share your thoughts, questions and suggest improvements:
Add Comment

You found us! Below are 50 most popular searched keywords

created at TagCrowd.com

Disclaimer: While every caution has been taken to provide our readers with most accurate information and honest analysis, please use your discretion before taking any decisions based on the information in this blog. Author will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.