sql - 如何在 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 transactioncommit 但没有它们我会收到一条错误消息,指出我需要定义事务的范围。我需要将这个最终过程部署到一个按计划运行的任务上,但不必合并来自两个过程的查询以及仍然处理流中当前数据的能力。

有什么办法可以实现吗?

最佳答案

根据@NickW 的评论,你不能让两个进程同时读取同一个流,因为运行 DML 命令正在改变该流 - 所以你的外部 SP 从流中选择并将锁定它直到外部SP 交易完成。您需要找到另一种方法来实现最终结果,例如在外部 SP 中将流写入表并在内部 SP 中使用该表。如果您使用临时表,它应该在任一 SP 运行时可用(因为它们将成为同一 session 的一部分),然后在 session 结束时自动删除 - 只要您不需要此数据2 SP 这可能更方便,因为您的维护工作更少。

https://stackoverflow.com/questions/71487445/

相关文章:

nestjs - 导出 NestJs 模块与导出提供程序

android - 无法连接到 frida 服务器 : need Gadget to attach

swift - AVPlayer 在 iOS 15.4 中寻求 completionHandler

c - 如何在不调用未定义行为的情况下检查值是否适合类型?

c# - 检测持续时间值列表中的闪烁

reactjs - 使用 useEffect 监听子组件可以吗?

amazon-web-services - 远程连接/SSH 到已部署的 AWS AppRunner

machine-learning - 如何使用 mlflow REST api 记录模型? mlfl

css - 如何简化这些 CSS 选择器?

python-3.x - SSHTunnel 搜索默认私钥 (id_rsa) 而不是我指定的 ssh