# Need some help with jinja trying to use a for loop over a run\_query result set from a seed table

**URL:** <https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272>\
**Category:** Help\
**Created:** [November 4, 2022, 1:41pm UTC](https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272 "2022-11-04T13:41:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremy.deats](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeremy.deats/32/1322_2.png) [@jeremy.deats](https://discourse.getdbt.com/u/jeremy.deats)\
**Post date:** [November 4, 2022, 1:41pm UTC](https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272/1 "2022-11-04T13:41:18Z")

</div>

Hi,

In the process of teaching myself dbt. Have run into a bit of a snag trying to iterate over a result set from a seed table. Seed table CSV is pretty basic here, just trying to prove the concept

The example code I’m following for this approach is taken from the dbt documentation on  
using run\_query(…) jinja function

| 1 | New York | |
| --- | --- | --- |
| 2 | Florida | |
| 3 | Oregon | |
| 4 | New Mexico | |
| 5 | Texas | |
| 6 | Colorado | |

```auto
{% set my_seed_query %}
select * from {{ ref('my_seed_table') }} 
{% endset %}
{% set results = run_query('select * from my_seed_query') %}
{% if execute %}
{% set results_sv_list = results.columns[1].values() %}
{% else %}
{% set results_sv_list = [] %}
{% endif %} 
{% for item in results_sv_list %}
 {{ print(item) }} 
{% endfor %}

```

I’ve tried some variation on this

```auto
with my_seed_query as (
 select * from {{ ref('my_seed_table') }}
)

{% set results = run_query('select * from my_seed_query') %}
{% if execute %}
{% set results_sv_list = results.columns[1].values() %}
{% else %}
{% set results_sv_list = [] %}
{% endif %}
{% for item in results_sv_list %}
    {{ print(item) }}
{% endfor %}

```

and also

```auto
{% set my_seed_query %}
 select * from {{ ref('my_seed_table') }}
{% endset %}
{% set results = run_query(my_seed_query) %}
{% if execute %}
{% set results_sv_list = results.columns[1].values() %}
{% else %}
{% set results_sv_list = [] %}
{% endif %}
{% for item in results_sv_list %}
    {{ print(item) }}
{% endfor %}

```

All of the above produce various errors

However, the code below works as expected and returns a result table, so the seed table is being resolved with ref(…) command, at least in the scenario below

```auto
with my_seed_query as (
 select * from {{ ref('my_seed_table') }}
)

select * from my_seed_query

```

---

<div class="post-metadata">

**Author:** ![obar1](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/obar1/32/1221_2.png) [@obar1](https://discourse.getdbt.com/u/obar1)\
**Post date:** [November 4, 2022, 4:17pm UTC](https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272/2 "2022-11-04T16:17:36Z")

</div>

hello  
check the compiled version of that ninja - you would see ref() DOES explode the actual name - while run\_query DOES NOT  
plus as beginner join slack `#advice-dbt-for-beginners`[Slack](http://app.slack.com/client/T0VLPD22H/CBSQTAPLG)

---

<div class="post-metadata">

**Author:** ![infl](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@infl](https://discourse.getdbt.com/u/infl)\
**Post date:** [November 5, 2022, 5:08pm UTC](https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272/3 "2022-11-05T17:08:31Z")

</div>

> [@jeremy.deats](#):
>
> `{% set results = run_query('select * from my_seed_query') %}`

It’s not clear what’s going on here, you’re trying to select \* from a variable, try this  
`{% set results = run_query(my_seed_query) %}`

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [November 5, 2022, 8:26pm UTC](https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272/4 "2022-11-05T20:26:16Z")

</div>

> [@jeremy.deats](#):
>
> various errors

This isn’t really enough to go on, so I’m going to skip over the sample code for now. Especially since it looks like your goal can be achieved via an existing library - the dbt utils package has a macro to get a column’s values: [get\_column\_values](https://github.com/dbt-labs/dbt-utils/#get_column_values-source)

You can either use it directly, or you could have a look at its source code to see how it’s implemented

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [February 16, 2024, 12:51am UTC](https://discourse.getdbt.com/t/need-some-help-with-jinja-trying-to-use-a-for-loop-over-a-run-query-result-set-from-a-seed-table/5272/5 "2024-02-16T00:51:22Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
