Jinja 中引用 DBT 变量

问题描述 投票:0回答:1

我在 dbt_project.yml 文件中有一个名为“my_ids”的项目变量。

vars: 
  my_ids: [1,2,3]

我正在尝试在模型 sql 文件中使用 jinja 引用此变量。

    {% for id in my_ids %}
      SELECT * FROM table1 Where table_id_col = {{id}}
      
      {% if not loop.last %}
         UNION ALL
      {% endif %}
    {% endfor %}

是否可以在 jinja {%%} 块中引用变量??

非常感谢。

M.

jinja2 dbt
1个回答
0
投票

我们可以在模型中使用项目变量 {{ var('...') }}。下面是您的情况

{% for id in var('my_ids') %}
      SELECT * FROM table1 Where table_id_col = {{id}}
      
      {% if not loop.last %}
         UNION ALL
      {% endif %}
{% endfor %}
© www.soinside.com 2019 - 2024. All rights reserved.