Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to Check if a table exists in the SQL Databases ?
Posted
Updated 29-Sep-13 12:52pm
v2

from a stored procedure?

SQL
create procedure check_table_exists
@aTableName varchar(50)
as
return (select count(*) from sysobjects where type = 'U' and name = @aTableName)
go
 
Share this answer
 
Comments
PIEBALDconsult 29-Sep-13 21:12pm    
You didn't include the schema name.
bryce 29-Sep-13 21:39pm    
exercise for the reader old chap ;)
1.
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;

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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900