# I am facing issue when trying to call a macro as a parameter and then passing it to a variable. Can anyone guide me where I am going wrong??

**URL:** <https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002>\
**Category:** Help\
**Created:** [October 4, 2022, 5:06pm UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002 "2022-10-04T17:06:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashishk9](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@ashishk9](https://discourse.getdbt.com/u/ashishk9)\
**Post date:** [October 4, 2022, 5:06pm UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/1 "2022-10-04T17:06:57Z")

</div>

![Wrong_code](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/a/a2c1ba69830d0eb421a4bc361975a9a89848b683.png)

In the correct\_code file, I am just hard coding the macro name under sql\_text.

But in the wrong\_code file, I am trying to pass the macro name to a variable and then using the variable name with the select command under sql\_text block. So it should ne read as a macro (which is : select {{poc1\_macro()) instead it is reading the whole thing as a variable itself and throwing an error:

```auto
syntax error line 1 at position 8 unexpected '{'.
  syntax error line 1 at position 19 unexpected '('.

```

---

<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:** [October 4, 2022, 11:13pm UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/2 "2022-10-04T23:13:14Z")

</div>

Hey @ashishk9, let me check I’m understanding you correctly:  
You want to dynamically call a macro, by taking the name of a macro and surrounding it with curly braces (and parentheses for an empty function signature)?

This won’t work because dbt only goes through one rendering pass - as you’ve seen, the compiled result will just be the string `{{poc1_macro()}}` instead of whatever `poc1_macro` does.

Can you explain why you’re trying to do this? There might be a different approach you can take.

---

<div class="post-metadata">

**Author:** ![ashishk9](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@ashishk9](https://discourse.getdbt.com/u/ashishk9)\
**Post date:** [October 6, 2022, 5:57am UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/3 "2022-10-06T05:57:35Z")

</div>

Hi @joellabes, I have three macros one which inserts data, one updates the table, and the last one is the main macro where I want to pass any of the two macros name as a parameter at a time. So that it performs only task which is insert or update.

---

<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:** [October 6, 2022, 6:06am UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/4 "2022-10-06T06:06:06Z")

</div>

dbt itself takes responsibility for doing inserts and updates, you shouldn’t need to be writing macros to be executing DDL etc.

Have you gone through the [getting started tutorial](https://docs.getdbt.com/guides/getting-started) or [read the dbt introduction](https://docs.getdbt.com/docs/introduction)? It may be helpful to get an indication of what dbt can do for you

---

<div class="post-metadata">

**Author:** ![ashishk9](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@ashishk9](https://discourse.getdbt.com/u/ashishk9)\
**Post date:** [October 6, 2022, 6:13am UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/5 "2022-10-06T06:13:57Z")

</div>

I have gone through dbt fundamentals. My requirement here is to see whether I can call a macro in another macro or not? insert or update is just an option not my end result.

But when I try to call the macro as parameter, it reads that as a variable.

---

<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:** [October 6, 2022, 6:21am UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/6 "2022-10-06T06:21:59Z")

</div>

You can call macros from other macros, but it can’t be totally dynamic.

For example you could do

```python
{% macro macro_1() %}
  {{ macro_2('hello') }}
{% endmacro %}

# in a separate file 
{% macro macro_2(msg) %}
  {{ msg }}
{% endmacro %}

```

and call macro\_1 from a sql file. But you can’t set which macro you want to call in a variable or pass that in as another argument.

That’s why I want to know your actual goal, so we can find an approach that _does_ work.

---

<div class="post-metadata">

**Author:** ![Surya](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/surya/32/3763_2.png) [@Surya](https://discourse.getdbt.com/u/Surya)\
**Post date:** [May 6, 2023, 11:58am UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/7 "2023-05-06T11:58:50Z")

</div>

Option-1:

U create a macro and pass macro name to it and call the macro using if else

```auto

{% macro macro_m1(macro_name) -%}
    {% if macro_name == 'table_1_macro' %}
        {{ table_1_macro() }}
    {% elif macro_name == 'table_2_macro' %}
        {{table_2_macro()}}
    {% endif %}
{%- endmacro %}

{% macro table_1_macro() -%}

{{print("inside table_1 macro")}}

{%- endmacro %}

{% macro table_2_macro() -%}

{{print("inside table_2 macro")}}

{%- endmacro %}

```

Option-2

I think u can also use adapter.dispatch to dynamically call a macro

> **[About dispatch config | dbt Developer Hub](https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch)**
>
> dbt extends functionality across data platforms using multiple dispatch.

---

<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:** [May 9, 2023, 10:06pm UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/8 "2023-05-09T22:06:17Z")

</div>

> [@Surya](#):
>
> I think u can also use adapter.dispatch to dynamically call a macro
> 
> [About dispatch config | dbt Developer Hub](https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch)

`adapter.dispatch` still requires the specific macro name to be specified in advance through a if/else block like your first example, i.e. you can’t do

```auto
{% set macro_name = 'my_macro' %}
{{ adapter.dispatch(macro_name)() }}

```

to call `my_macro()`.

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [May 11, 2023, 4:54am UTC](https://discourse.getdbt.com/t/i-am-facing-issue-when-trying-to-call-a-macro-as-a-parameter-and-then-passing-it-to-a-variable-can-anyone-guide-me-where-i-am-going-wrong/5002/9 "2023-05-11T04:54:55Z")

</div>

I am learning about sources in dbt. My understanding the sources define raw table location like database, schema, table name and so on, so we don’t hardcode the table name in the models (\*.sql) . In the model I still define

Note: `@Ram` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1683780888894059?thread_ts=1683374355.974029&cid=CBSQTAPLG). It might not have transferred perfectly.
