Files
mars-elt/dbt/macros/data_transform/sequence_next_value.sql
Grzegorz Michalski 2c225d68ac init
2026-03-02 09:47:35 +01:00

33 lines
964 B
MySQL

{% macro sequence_next_value(sequence_name, check_disable=false) %}
{# macro checks if a function for this sequence exists,
if it does, the function is used
if not, the standard command is used
check can be disabled by passing true in the 2nd paramater
#}
{% if execute %}
{% if not check_disable %}
{% set get_func %}
select count(*)
from all_objects
where object_type = 'FUNCTION'
and status = 'VALID'
and owner || '.' || object_name = upper('{{ sequence_name }}_fnc')
{% endset %}
{% set result = run_query(get_func) %}
{% set fnc_count = result.columns[0].values()[0] %}
{% else %}
{% set fnc_count = 0 %}
{% endif %}
{% if fnc_count > 0 %}
{% set nextvalue = sequence_name ~ '_fnc()' %}
{% else %}
{% set nextvalue = sequence_name ~ '.nextval'%}
{% endif %}
{{ return(nextvalue) }}
{% endif %}
{% endmacro %}