Prefix Columns Maco

A self-explanatory macro for prefixing all columns.

{%- macro prefix_columns(ref, prefix) -%}
    -- prefixes all columns in ref
    {%- set cols = adapter.get_columns_in_relation(ref) -%}

    -- select all columns from ref1
    select
    {% for col in cols %}
        {% if not loop.last %}
            table1.{{col.name}} as {{prefix}}{{col.name}},
        {% else %}
            table1.{{col.name}} as {{prefix}}{{col.name}}
        {% endif %}
    {% endfor %}
    from
    {{ ref }} as table1
{%- endmacro -%}
4 Likes

Hey @kevincmclaughlin. That’s always a good use case.
I guess the utils star macro could be used as well. Its a bit more powerful.
For further macros I’d also recommend to compile your code in action to see how blank lines and comments turn out. A bit of “-%}” and the distinction between sql comments (–,/**/) and jinja comments ({##}) could improve the compiled sql.