I am wondering if this is possible. I have a fairly complex SQL statement that uses about 6 UNIONs to pull data from multiple tables and combine it together (MySQL database). The problem is that some databases I want to run the statement on, do not have all the tables yet. They are created over time. When I run it on a database without all the tables, I get a select error for my username because the tables do not exist. I personally do not have control over the databases to put the missing tables in earlier than they are normally created.
I want to create a statement that can be used on new databases that do not have some tables without having to manipulate it with other code if possible. Not all the code will run from VB.NET.
In VB I could just do a test to see if a table exists and concatenate the UNIONs but I will be running it from a program where I need to just do it all with the sql statement and I don't want to have to create multiple reports based on different queries and make the users figure out which one to run.
So a dumbed down example would be something like:
However, while Orders and Orders1 always exist, Orders2 and Orders3 only exist after a couple of years and are used for archival.
So I would like to do some sort of CASE statement but I don't know where to start with the syntax. In pseudocode, it would run something like:
I've been reading about pulling the table exists information from the information_schema. So would I just try something like:
Has anyone come across this kind of thing before and can give me some pointers? Am I on the right track here?
TIA, rasinc
I want to create a statement that can be used on new databases that do not have some tables without having to manipulate it with other code if possible. Not all the code will run from VB.NET.
In VB I could just do a test to see if a table exists and concatenate the UNIONs but I will be running it from a program where I need to just do it all with the sql statement and I don't want to have to create multiple reports based on different queries and make the users figure out which one to run.
So a dumbed down example would be something like:
Code:
Select InvDate, Amount from Orders
UNION ALL
Select InvDate, Amount from Orders1
UNION ALL
Select InvDate, Amount from Orders2
UNION ALL
Select InvDate, Amount from Orders3
So I would like to do some sort of CASE statement but I don't know where to start with the syntax. In pseudocode, it would run something like:
Code:
Select InvDate, Amount from Orders
UNION ALL
Select InvDate, Amount from Orders1
CASE WHEN Orders2 EXISTS THEN
UNION ALL
Select InvDate, Amount from Orders2
END
CASE WHEN Orders3 EXISTS THEN
UNION ALL
Select InvDate, Amount from Orders3
END
Code:
Select InvDate, Amount from Orders
UNION ALL
Select InvDate, Amount from Orders1
CASE WHEN (SELECT table_name
FROM information_schema.tables
WHERE table_schema = databasename
AND table_name = Orders1) = Orders1
THEN
UNION ALL
Select InvDate, Amount from Orders2
END
CASE WHEN (SELECT table_name
FROM information_schema.tables
WHERE table_schema = databasename
AND table_name = Orders2) = Orders2
THEN
UNION ALL
Select InvDate, Amount from Orders3
END
TIA, rasinc