Hello
I am using dbt Core on Snowflake. I have some SQL that I am trying to execute via on on-run-start hook.
The SQL uses Snowflake SQL scripting because it contains some conditional logic. I have embedded the SQL in a macro that looks like:
{% macro example_on_run_start() %}
{% set sql %}
DECLARE
EXAMPLE_VAR INT;
BEGIN
SELECT COUNT(*) INTO :EXAMPLE_VAR
FROM ...
WHERE ...;
IF EXAMPLE_VAR = 0 THEN
...
...
END IF;
END;
{% endset %}
{{ return(sql) }}
{% endmacro %}
When dbt executes the above, it treats the SQL as multiple standalone statements, splitting the SQL at each semi-colon. It first executes:
DECLARE
EXAMPLE_VAR INT
Clearly this isn’t a valid standalone SQL statement so it fails. The whole block of SQL needs to be executed in one query to Snowflake.
Is there any way to get dbt to execute the entire SQL block in one go? I.e. not split up the SQL.
Thanks