sqldbt

How do I loop through alll columns using Jinja in DBT?


I want to iterate over all the columns using dbt.


Solution

  • I think the star macro from the dbt-utils package + some for-loop logic might help you here? This depends on the exact use case and warehouse you're using (as pointed out in the comments).

    The star macro generates a list of columns in the table provided.

    So a possible approach would be something along the lines of:

    {% for col in adapter.get_columns_in_relation(ref('my_model')) }}] %}
    ...operation...
    {% endfor %}