How to declare and init variable in a dbt model in `.sql` file with big query adaptor?

Viewed 3764

I would like to declare and init a variable in a dbt model customer.sql file. I used the keyword DECLARE to declare a variable like the BigQuery documentation suggests but I got a Syntax error on DECLARE keyword.

Code:

DECLARE myDate VARCHAR DEFAULT '2021-01-01';

with order_bis as (

    select
        order_id

    from
        order
    where
        customer_date > myDate

)

select * from order_bis

Error: Syntax error: Expected "(" or keyword SELECT or keyword WITH but got keyword DECLARE ...

1 Answers

It seems that using SQL variables does not work "yet" with dbt. You can use Jinja variable if you want to use static values in multiple places, so that you can rely on Jinja logic.

{% set myVar = '2017-01-01' %}

...

where
        customer_date > {{myVar}}

...
Related