# Call macro within another macro

**URL:** https://discourse.getdbt.com/t/call-macro-within-another-macro/12976
**Category:** Help
**Tags:** jinja, macros
**Created:** [April 19, 2024, 5:47pm UTC](https://discourse.getdbt.com/t/call-macro-within-another-macro/12976 "2024-04-19T17:47:59Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![needlearn](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@needlearn](https://discourse.getdbt.com/u/needlearn)
#### Post date: [April 19, 2024, 5:47pm UTC](https://discourse.getdbt.com/t/call-macro-within-another-macro/12976/1 "2024-04-19T17:47:59Z")

</div>

## Need to pass the value from a DB query in one macro and call this first macro inside another macro to pass the values dynamically.

## Trying to read values from a databricks table through macro1 and passing them in source yml creation macro to populate values.

```auto
   {% do sources_yaml.append(" tables:") %}

    {% if table_names is none %}
        {% set tables = codegen.get_tables_in_schema(
            schema_name, database_name, table_pattern, exclude
        ) %}
    {% else %} {% set tables = table_names %}
    {% endif %}

    {% for table in tables %}
        {% do sources_yaml.append(" - name: " ~ table | lower) %}
        {% if include_descriptions %}
            {% do sources_yaml.append(' description: ""') %}
        {% endif %}

        {% if generate_columns %}
            {% do sources_yaml.append(" columns:") %}
            {% set table_relation = api.Relation.create(
                database=database_name, schema=schema_name, identifier=table
            ) %}

            {% set columns = adapter.get_columns_in_relation(table_relation) %}

            {% for column in columns %}
                {% do sources_yaml.append(" - name: " ~ column.name | lower) %}
                {% if include_descriptions %}
                    {% do sources_yaml.append(' description: ""') %}
                {% endif %}
                {% if include_samplevalue %}
                    {%- set tblnm = '{table}' -%}
                    {%- set colnm = '{column}' -%}
                    
                    adapter.execute_macro('generate_macro1', table_nm='arg1', column_nm = 'arg2')
                    {%- do sources_yaml.append(" tag_value: " ~ reqdval ) -%}
                {% endif %}
            {% endfor %}
            {% do sources_yaml.append("") %}

        {% endif %}

    {% endfor %}

```

I need to pass values dynamically for arg1,arg2 to invoke the macro1 and read the results for populating the variable ‘reqdval’.

---

<div class="post-metadata">

### Author: ![Priest](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@Priest](https://discourse.getdbt.com/u/Priest)
#### Post date: [July 31, 2025, 12:43pm UTC](https://discourse.getdbt.com/t/call-macro-within-another-macro/12976/2 "2025-07-31T12:43:52Z")

</div>

Is this something similar?

> [@Rendering Macro within Macro](https://discourse.getdbt.com/t/rendering-macro-within-macro/20001):
>
> The problem I’m having Hi, I’m trying to dynamically build the columns in the “SELECT” query utilizing a customer macro and the model’s YML file. And I’m getting ‘macro’ undefined error. The context of why I’m trying to do this This gives the users to control the value for the column that they want. Even though we declare a standard, that might be satisfactory to the client and this enables them to provide overwrites to a specific column. What I’ve already tried Desired YML Structure that t…
