tags:

views:

16625

answers:

5

I would like this to be the ultimate discussion on how to check if a table exists in SQL Server 2000/2005 using SQL Statement.

When you Google for the answer, you get so many different answers. Is there an official/backward & forward compatible way of doing it?

Here are two ways to start discussion:

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;

IF OBJECT_ID (N'".$table_name."', N'U') IS NOT NULL SELECT 1 AS res ELSE SELECT 0 AS res;

MySQL provides a nice SHOW TABLES LIKE '%tablename%'; statement. I am looking for something similar.

+7  A: 

Using the Information Schema is the SQL Standard way to do it, so it should be used by all databases that support it.

Vinko Vrsalovic
+5  A: 

We always use the OBJECT_ID style for as long as I remember

IF OBJECT_ID('*objectName*') IS NOT NULL
Bob King
That's what it's for. +1
ConcernedOfTunbridgeWells
I believe this would be fast, though not very portable. Information schema views are guaranteed to exist on any DBRMS that supports the standard. Furthermore, plain OBJECT_ID doesn't guarantee the object's a table.
Joe Pineda
+20  A: 

For queries like this it is always best to use an INFORMATION_SCHEMA view. These views are (mostly) standard across many different databases and rarely change from version to version.

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
akmad
Works great! In T-SQL (in response to the original poster), though, it's TABLE_SCHEMA, not SCHEMA_NAME. Thanks for the tip.
Nicholas Piasecki
How would you avoid having schema_name in the where clause? The benefit of using object_id() approach is that it defaults to the current schema.
haridsv
Given that an object name alone (that is, without a schema) is not guaranteed to be unique, there is no 100% failsafe way to do this. If you are working with a DB that has no naming conflicts across schemas then simply omitting the "TABLE_SCHEMA = 'TheSchema'" will work just fine.
akmad
To check for a temporary table, we have to query the tempdb database and use a LIKE operator for the table name `SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'TheSchema' AND TABLE_NAME LIKE '#TheTable%'`
Pierre-Alain Vigeant
A: 

If you need to work on different databases:

DECLARE @Catalog VARCHAR(255)
SET @Catalog = 'MyDatabase'

DECLARE @Schema VARCHAR(255)
SET @Schema = 'dbo'

DECLARE @Table VARCHAR(255)
SET @Table = 'MyTable'

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES   
    WHERE TABLE_CATALOG = @Catalog 
      AND TABLE_SCHEMA = @Schema 
      AND TABLE_NAME = @Table))
BEGIN
   --do stuff
END
Even Mien
+5  A: 

Also note that if for any reason you need to check for a temporary table you can do this:

if OBJECT_ID('tempdb..#test') is not null
 --- temp table exists
jbloomer