Reputation: 129
I am trying to use a variable passed in dbt run command in a macro. How can I use it
{% set override_schema_name = vars('override_schema_name') %}
{%- set default_schema = target.schema -%}
{%- if custom_schema_name is none -%}
{{ default_schema }}
{%- else -%}
{{ default_schema }}_{{ override_schema_name| trim }}
{%- endif -%}
{%- endmacro %}
In this example i am running dbt run --vars '{"override_schema_name":"someschema"}', but this is throwing error that variable is not defined.
Upvotes: 3
Views: 16180
Reputation: 5805
There are a few things wrong in your code:
The macro name to access the value of a variable is var
, not vars
. So your first line should be {% set override_schema_name = var('override_schema_name') %}
You should declare any vars in your project.yml
file before using them in a macro or elsewhere in your project:
name: my_dbt_project
version: 1.0.0
config-version: 2
# Define variables here
vars:
override_schema_name: something
Alternatively, you can provide a default value to your var
call in the macro:
{% set override_schema_name = var('override_schema_name', target.schema) %}
After making those changes, your dbt run
command should work.
var
docs: https://docs.getdbt.com/reference/dbt-jinja-functions/var
"Using Variables" guide: https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-variables
Upvotes: 5