for the below code, I have deleted the hist.game_hist table from db.
Now, according to the code below, I want my code to pickup the else condition and avoid going into the then condition (as game_hist does not exists, it will throw error).
But Till now, when I try to run this code, I get the error that, from then condition "hist.game_hist does not exists".
SELECT
pi.*
FROM stg.stg_game pi
WHERE pi.status != '9'
AND pi.hvr_capture_timestamp::timestamp >
(CASE
WHEN EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'hist' AND table_name = 'game_hist'
)
THEN COALESCE(
(SELECT max(dbt_updated_at) - INTERVAL '120 minutes' FROM hist.game_hist),
'1900-01-01'::timestamp
)
ELSE '1900-01-01'::timestamp
end)
for the above code, I have deleted the hist.game_hist table from db.
Now, according to the code below, I want my code to pickup the else condition and avoid going into the then condition (as game_hist does not exists, it will throw error).
But Till now, when I try to run this code, I get the error that, from then condition "hist.game_hist does not exists".
2
Answers
The problem you are facing comes from the database’s attempt to assess the subquery in the THEN condition even when table doesn’t exist. You can conditionally run the subquery only if the table exists by using dynamic SQL to prevent this.
Try this updated code. This code conditionally executes the subquery only if the table exists using a PL/pgSQL block with dynamic SQL. It immediately sets max_dbt_updated_at to the default value if the table is not found, preventing the issue you were experiencing.
Hope it’s helpful 🙂
The following query produces the desired results the OP described:
This approach takes advantage of
QUERY_TO_XML
‘s ability to execute dynamic SQL. Because it is a SQL query instead of a PL/pgSQL anonymous block, it can be used directly as part of a larger SQL query. Ifhist.game_hist
doesn’t exist, then the SELECT list in theinfromation_schema.tables
subquery is not evaluated, and the subquery returnsNULL
. This query also avoids repeating the default value for the capture timestamp comparison.