I am working with N tables in a Snowflake warehouse where N will grow arbitrarily large overtime.
I am creating a SQL statement dynamically based on the list of tables, the SQL script looks something like:
SELECT * FROM table_1
UNION ALL
SELECT * FROM table_2
UNION ALL
...
SELECT * FROM table_n;
The problem is that the list of tables is itself set dynamically and some tables may not exist. In the case a table does not exist I want to simply ignore it in my select statement. So something like:
SELECT * FROM table_1 IF EXISTS table_1
UNION ALL
SELECT * FROM table_2 IF EXISTS table_2
UNION ALL
...
SELECT * FROM table_n IF EXISTS table_n;
Does anyone know if this behavior is achievable in SQL / Snowflake?
For the record, I am using the templating system dbt to loop over each table and join them together in a single table.