Skip to content

Commit

Permalink
Clarify statement blocks macro need ref (#5138)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirnawong1 authored Mar 28, 2024
2 parents 1945b27 + 256cb73 commit 3965bb7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions website/docs/reference/dbt-jinja-functions/statement-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ We recommend using the [`run_query` macro](/reference/dbt-jinja-functions/run_qu
<File name='get_states_statement.sql'>

```sql
-- depends_on: {{ ref('users') }}

{%- call statement('states', fetch_result=True) -%}

select distinct state from {{ ref('users') }}
Expand All @@ -31,6 +33,42 @@ The signature of the `statement` block looks like this:
statement(name=None, fetch_result=False, auto_begin=True)
```

When executing a `statement`, dbt needs to understand how to resolve references to other dbt models or resources. If you are already `ref`ing the model outside of the statement block, the dependency will be automatically inferred, but otherwise you will need to [force the dependency](/reference/dbt-jinja-functions/ref#forcing-dependencies) with `-- depends_on`.

<expandable alt_header="Example using -- depends_on">

```sql
-- depends_on: {{ ref('users') }}

{% call statement('states', fetch_result=True) -%}

select distinct state from {{ ref('users') }}

/*
The unique states are: {{ load_result('states')['data'] }}
*/
{%- endcall %}
```
</expandable>

<expandable alt_header="Example using ref() function">

```sql

{% call statement('states', fetch_result=True) -%}

select distinct state from {{ ref('users') }}

/*
The unique states are: {{ load_result('states')['data'] }}
*/

{%- endcall %}

select id * 2 from {{ ref('users') }}
```
</expandable>

__Args__:
- `name` (string): The name for the result set returned by this statement
- `fetch_result` (bool): If True, load the results of the statement into the Jinja context
Expand Down

0 comments on commit 3965bb7

Please sign in to comment.