首页 > 解决方案 > 雪花存储过程从 dbt 失败

问题描述

我在通过 dbt 在 Snowflake 中执行存储过程时遇到问题:

我的过程的描述是这样的: MyStoredProcedure(ARRAY, VARCHAR, VARCHAR)

所以,当我想运行它时,我使用array_construct函数来创建第一个参数,例如: call MyStoredProcedure(array_construct(array_construct('str_1', 'str_2')), 'schema_name', 'table_name');

这在我在雪花中运行时有效。但是,当我从 dbt 运行它时,它会因以下错误而失败:

不允许修改在不同范围开始的事务。

我确信这与在此调用中调用 array_construct 有关。

我应该提到,要从 dbt 运行它,我已经定义了一个这样的宏:

{% macro MyStoredProcedure() %}
    {% set query -%}
        CALL MyStoredProcedure(
           array_construct(array_construct('str_1', 'str_2')),
           'schema_name',
           'table_name');
    {%- endset %}

    {% do run_query(query) %}
{% endmacro %}

当然像这样运行它:dbt run-operation MyStoredProcedure

我感谢任何提示或想法来帮助我解决这个问题。

谢谢

标签: stored-proceduresdbtsnowflake-sql

解决方案


我仍然非常了解雪花,但是通过阅读文档的这一部分 ,在我看来这可能只是您的多范围过程调用的机制。

据我所知,在雪花中执行此操作的典型(非 dbt)方法是:

begin transaction;
insert 1; 
call storedprocedure();
insert 2;
commit;

也许您可以像这样调整宏,这将克服范围嵌套问题,因为一切都将在一个事务中完成?

{% macro MyStoredProcedure() %}

    {% set query -%}
        begin transaction;
        CALL MyStoredProcedure(
           array_construct(array_construct('str_1', 'str_2')),
           'schema_name',
           'table_name');
        commit;
    {%- endset %}

    {% do run_query(query) %}
{% endmacro %}

无法对此进行测试,因为我无法重现该过程本身。


推荐阅读