1

This query generates an error because table2 doesn't exist:

Select * FROM table WHERE table2.id IS NOT NULL 

Is there anything like this for check the table2 before apply the check on the id?

Select * FROM table WHERE (EXIST(table2) AND table2.id IS NOT NULL) or not EXIST(table2) 
1
  • My SQL may be rusty but how do you reference "table2" when you did not include it in the FROM portion of the query? Commented Jun 2, 2010 at 14:28

2 Answers 2

3

You need to query this system table:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'yourdatabasename' AND table_name = 'table2'; 

If a row is returned, then your table exists.

Sign up to request clarification or add additional context in comments.

Comments

0

I do not believe there is any command or function in standard SQL to do this. You could query the data dictionary to check if the table exists before issuing your SQL query as follows:

SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name = 'xxx';

I do not think it could be done in a single SQL statement though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.