954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Checking if a table exists in a db

hi,
Is there any sql query to check if a table exists already in a database?
Thanks in advance.:)

vs.vaidyanathan
Newbie Poster
23 posts since May 2009
Reputation Points: 28
Solved Threads: 0
 
select count(*) from information_schema.tables where table_name='SEARCH_TABLE_NAME' AND TABLE_SCHEMA='SEARCH_DATABASE_NAME'

OR
If you want to create new table you may also use

create or replace syntax........

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

OR

...

CREATE TABLE IF NOT EXISTS tbl_name
tyson.crouch
Junior Poster
152 posts since Apr 2010
Reputation Points: 16
Solved Threads: 17
 

Be aware that other database engines provide this information in other ways. So far as I know, every engine gives you some way to do this programmatically; and so far as I know, every database engine has associated documentation about how to do it.

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

OR

...

CREATE TABLE IF NOT EXISTS tbl_name


This doesn't seem to work... reports a syntax error near IF.hmmmm....:-/

vs.vaidyanathan
Newbie Poster
23 posts since May 2009
Reputation Points: 28
Solved Threads: 0
 

getting msg 156, level 15, state 2
syntax error near keyword 'if'
msg 102, level 15, state 1
syntax error near keyword 'exists'
So tried the workaround at microsoft which suggested i put the semicolon on the previous statements, that only threw up the same error twice more.The microsoft site says something about a compatiblity issue which i'm not sure i understand. Some help pl?

vs.vaidyanathan
Newbie Poster
23 posts since May 2009
Reputation Points: 28
Solved Threads: 0
 
Be aware that other database engines provide this information in other ways. So far as I know, every engine gives you some way to do this programmatically; and so far as I know, every database engine has associated documentation about how to do it.

CREATE TABLE abc
Msg 102, Level 15, State 1
Server 'XYZ', Line 1
Incorrect syntax near 'abc'.
DB-Library error:
General SQL Server error: Check messages from the SQL Server.
Tried with and without the ; on this line as well as the previous query. Nothing seems to work... frusty...

vs.vaidyanathan
Newbie Poster
23 posts since May 2009
Reputation Points: 28
Solved Threads: 0
 
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

@raja thanks for the link. It was informative but the site talks about MySQL statements. My requirement is a server program.

vs.vaidyanathan
Newbie Poster
23 posts since May 2009
Reputation Points: 28
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: