dbt - Documentation


New in dbt v0.14.0

The run_query macro is new in dbt v0.14.0

The run_query macro provides a convenient way to run queries and fetch their results. It is a wrapper around the statement block, which is more flexible, but also more complicated to use.


  • sql: The SQL query to execute

Returns a Table object with the result of the query. If the specified query does not return results (eg. a DDL, DML, or maintenance query), then the return value will be none.

Note: The run_query macro will not begin a transaction automatically - if you wish to run your query inside of a transaction, please use begin and commit statements as appropriate.

Example Usage:

{% set results = run_query('select 1 as id') %}
{% do results.print_table() %}

-- do something with `results` here...
{% macro run_vacuum(table) %}

  {% set query %}
    vacuum table {{ table }}
  {% endset %}
  {% do run_query(query) %}
{% endmacro %}


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.