如何在 Snowflake 中嵌套使用一个流的存储过程?
How to nest stored procedures that use one stream in Snowflake?
我有以下架构:
- 主存储过程
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 中将流写入 table 并在内部 SP 中使用该 table 。如果您使用临时 table 它应该在任一 SP 为 运行 时可用(因为它们将是同一会话的一部分)然后在会话结束时自动删除 - 只要您不这样做在 2 个 SP 之外需要此数据,这可能更方便,因为您的维护工作较少。
我有以下架构:
- 主存储过程
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 中将流写入 table 并在内部 SP 中使用该 table 。如果您使用临时 table 它应该在任一 SP 为 运行 时可用(因为它们将是同一会话的一部分)然后在会话结束时自动删除 - 只要您不这样做在 2 个 SP 之外需要此数据,这可能更方便,因为您的维护工作较少。