Hello, is there any way to create a global variable in dbt core and initializing it with a select query from DB?
I just want to read a parameter value from a parameter table and use it in my model, several times
Thanks
Liz
Hello, is there any way to create a global variable in dbt core and initializing it with a select query from DB?
I just want to read a parameter value from a parameter table and use it in my model, several times
Thanks
Liz
U can use run_query macro to execute a query on warehouse and access the results
{% set results = run_query('select parameter from param_table') %}
{% if execute %}
{# Return the first column #}
{% set global_variable = results.columns[0].values() %}
{% else %}
model1.sql
select col1, col2 from table_1 where col1= {{global_variable}}
If I used the first part in my first model
Lets say:
Model 1.sql
{% set results = run_query(‘select parameter from param_table’) %}
{% if execute %}
{# Return the first column #}
{% set global_variable = results.columns[0].values() %}
{% else %}
can I use this variable {{global_variable}}from other models (model2.sql,model3.sql…)?
Hi @Liz , In dbt you can define the global variable which you can use in any models. Inside dbt_project.yml file you will see vars section there you can define your global variable.
vars:
drop_old_relations: ['MARTS', 'TRANSFORM']
To call the variable you can use :
{{ var('drop_old_relations') }}
no u cannot use like that
its not possible to initialize a variable with a select query from DB