views:

43

answers:

2

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)

Thanks

+3  A: 

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.

LesterDove
A: 

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.

Chris