我有以下架构:
- 主存储过程
main_sproc
- 嵌套存储过程
nested_sproc
我们的任务是在 Snowflake 中处理来自 stream
的数据,但必须通过嵌套方法来完成。
因此这将是代码:
create or replace procedure nested_sproc()
returns varchar
language sql
as
begin
begin transaction;
insert into table1
select * from stream; -- it will be more complex than this and multiple statements
commit;
end;
create or replace procedure main_sproc()
returns varchar
language sql
as
begin
begin transaction;
insert into table1
select * from stream; -- it will be more complex than this and multiple statements
call nested_sproc();
commit;
end;
当我尝试运行 call main_sproc()
时,我注意到第一条语句通过了,但是当它到达 call nested_sproc()
时事务被阻止。我想这是因为我在两个过程中都有一个 begin transaction
和 commit
但没有它们我会收到一条错误消息,指出我需要定义事务的范围。我需要将这个最终过程部署到一个按计划运行的任务上,但不必合并来自两个过程的查询以及仍然处理流中当前数据的能力。
有什么办法可以实现吗?
最佳答案
根据@NickW 的评论,你不能让两个进程同时读取同一个流,因为运行 DML 命令正在改变该流 - 所以你的外部 SP 从流中选择并将锁定它直到外部SP 交易完成。您需要找到另一种方法来实现最终结果,例如在外部 SP 中将流写入表并在内部 SP 中使用该表。如果您使用临时表,它应该在任一 SP 运行时可用(因为它们将成为同一 session 的一部分),然后在 session 结束时自动删除 - 只要您不需要此数据2 SP 这可能更方便,因为您的维护工作更少。
关于sql - 如何在 Snowflake 中嵌套使用一个流的存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71487445/