Check if table exists in SQL Server

0 votes

I want this to be the last word on how to use SQL Statements in SQL Server 2000/2005 to determine whether a table is present.

Here are two options for how to go about it. Which one is the accepted practice or ideal method?

First way:

IF EXISTS (SELECT 1 
           FROM INFORMATION_SCHEMA.TABLES 
           WHERE TABLE_TYPE='BASE TABLE' 
           AND TABLE_NAME='mytablename') 
   SELECT 1 AS res ELSE SELECT 0 AS res;

Second way:

IF OBJECT_ID (N'mytablename', N'U') IS NOT NULL 
   SELECT 1 AS res ELSE SELECT 0 AS res;

MySQL provides the simple:

SHOW TABLES LIKE '%tablename%'; 

The statement. I am looking for something similar.

Aug 21, 2022 in Database by Kithuzzz
• 38,000 points
39,556 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

It is always advisable to utilise an INFORMATION SCHEMA view for queries like these. These views seldom vary from version to version and are (mostly) common across many different databases.

To check if a table exists use:

IF (EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'TheSchema' 
                 AND  TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END

I hope this helps you.

answered Aug 22, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Database

0 votes
0 answers

Check if table exists and if it doesn't exist, create it in SQL Server 2008

I'm using SQL Server 2008 to create ...READ MORE

Sep 2, 2022 in Database by Kithuzzz
• 38,000 points
1,032 views
0 votes
1 answer

How to check if a table exists in a given schema

Since you havent mentioned the DBMS in ...READ MORE

answered Oct 4, 2019 in Database by Daric
• 500 points
2,919 views
0 votes
0 answers

What's the difference between a temp table and table variable in SQL Server?

We can build temp tables in SQL ...READ MORE

Feb 25, 2022 in Database by Vaani
• 7,070 points
705 views
0 votes
0 answers

What is the difference between drop table and delete table in SQL Server?

What is the distinction between the following ...READ MORE

Aug 9, 2022 in Database by Kithuzzz
• 38,000 points
1,019 views
0 votes
0 answers

How can I show the table structure in SQL Server query?

Query: SELECT DateTime, Skill, Name, TimeZone, ID, User, ...READ MORE

Aug 11, 2022 in Database by Kithuzzz
• 38,000 points
880 views
0 votes
0 answers

Selecting COUNT(*) with DISTINCT

In SQL Server 2005, I have a ...READ MORE

Feb 14, 2022 in Database by Neha
• 9,020 points
580 views
0 votes
0 answers

Simple DateTime sql query

How do I query the DateTime database ...READ MORE

Aug 12, 2022 in Database by Kithuzzz
• 38,000 points
699 views
0 votes
0 answers

What represents a double in sql server?

I have several properties in C# which ...READ MORE

Aug 13, 2022 in Database by Kithuzzz
• 38,000 points
827 views
0 votes
0 answers

How to get Time from DateTime format in SQL?

I want to use a SQL query ...READ MORE

Aug 15, 2022 in Database by Kithuzzz
• 38,000 points
1,011 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP