如何从SQL存储过程调用外部存储过程并处理提交控制

发布于 2025-01-17 07:24:40 字数 1077 浏览 4 评论 0原文

我试图通过传递两个参数从 Sql 存储过程调用外部存储过程。其中一个参数预计会返回一个字符串值,当该值到达时,我需要停止将数据提交到任何文件中。

解释一下情况 - SP1(SQL存储过程)将调用SP2(外部存储过程),SP2将调用RPGLE程序PGM1,后者将调用另一个RPGLE程序PGM2。

现在我的任务是处理 SP1 中 PGM2 中使用的 File1 和 File 2 的提交控制。如果在任何时候更新 File1 并且 File2 在更新任何记录时出错,则文件 1 中的数据也应该回滚。但这种回滚应该发生在 SP1 中。

到目前为止,我已尝试将此问题分为两部分 -

PARTA - 如何从 SQL 存储过程调用外部存储过程。

PARTB - 本质上如何处理SQL存储过程中的承诺,如果PGM2返回错误数据应该回滚。

下面是到目前为止我尝试过的代码片段。但运气不好。

CREATE OR REPLACE PROCEDURE MYLIB.SP1 (    
                    IN PRINPUT CHAR(1200) ,       
                    INOUT PRERR CHAR(50) )        
SPECIFIC MYLIB.SP1                         
BEGIN                                             
  DECLARE SQLSTATE CHAR(5) DEFAULT ' ';           
  DECLARE SQLCODE INTEGER DEFAULT 0;              
    CALL MYLIB.SP2(PRINPUT, PRERR);      
      IF SQLCODE = 0 THEN                         
        COMMIT;                                   
      ENDIF;                                      
END 

任何建议/指导表示赞赏。

I am trying to call a external stored procedure from Sql stored procedure by passing two parameters. one of the parameters is expected to return back with a string value, which when arrives I need to stop the data to be committed in any file.

To explain the situation - SP1 (SQL stored procedure) will call SP2 (External stored procedure), which will call RPGLE program PGM1, which will call another RPGLE program PGM2.

Now I am tasked to handle commitment control of File1 and File 2 used in PGM2 from SP1. If at any point File1 is updated and File2 gives an error while updating any record, data from File 1 should also be rolled back. but this rollback should happen in SP1.

So far I have tried to split this issue in two parts-

PARTA - How to Call External stored procedure from SQL stored procedure.

PARTB - How to handle commitment in SQL stored procedure in essence, if PGM2 gives back error data should be rolled back.

Below is the piece of code so far I have tried. But have no luck.

CREATE OR REPLACE PROCEDURE MYLIB.SP1 (    
                    IN PRINPUT CHAR(1200) ,       
                    INOUT PRERR CHAR(50) )        
SPECIFIC MYLIB.SP1                         
BEGIN                                             
  DECLARE SQLSTATE CHAR(5) DEFAULT ' ';           
  DECLARE SQLCODE INTEGER DEFAULT 0;              
    CALL MYLIB.SP2(PRINPUT, PRERR);      
      IF SQLCODE = 0 THEN                         
        COMMIT;                                   
      ENDIF;                                      
END 

Any suggestion/Guidance is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2025-01-24 07:24:40

试试这个我的朋友:
关于 SP1 的

BEGIN                                             
  DECLARE SQLSTATE CHAR(5) DEFAULT ' ';           
  DECLARE SQLCODE INTEGER DEFAULT 0;    
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;         
    CALL MYLIB.SP2(PRINPUT, PRERR);      
      IF SQLCODE = 0 THEN                         
        COMMIT;
      ELSE 
        ROLLBACK;                          
      ENDIF;                                      
END 

答案:

PARTA:如果 Ext Stored Proc 存在,按照您的调用方式,它是正确的。

PARTB:设置的事务范围涵盖从发出时刻到执行提交或回滚的所有更改的行。请记住这一点,每个 SQL 存储过程都在 *caller actgrp 上运行,因此,您需要检查您的 RPG 程序是否也在 *caller 上运行。

最后,我上次测试时,恐龙仍然在地球上行走,对 SQL 存储过程的提交范围限定了在 RPG 内调用的 RPG 程序所做的更改,但 RPG STRCMTCTL 没有获取在 RPG 内调用的 SQL 存储过程进行的更改。

玩得开心!

statements-set-transaction

control-example-using-transaction-logging-file-start-application

定义-示例-jobs-commitment-definitions

Try this my friend:
On SP1

BEGIN                                             
  DECLARE SQLSTATE CHAR(5) DEFAULT ' ';           
  DECLARE SQLCODE INTEGER DEFAULT 0;    
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;         
    CALL MYLIB.SP2(PRINPUT, PRERR);      
      IF SQLCODE = 0 THEN                         
        COMMIT;
      ELSE 
        ROLLBACK;                          
      ENDIF;                                      
END 

Answers:

PARTA: If the Ext Stored Proc exists, the way you're calling it, it's correct.

PARTB: The set transaction scopes all the rows changed from the moment is issued, to the execution of commit or rollback. Remember this, every SQL Stored proc runs on *caller actgrp, so, you need to check if your RPG program runs on *caller too.

Finally, last time I tested, dinos still walked the earth, the commit on SQL Stored Proc scoped the changes made with a RPG program called within, but the RPG STRCMTCTL doesn't get the changes made on SQL Stored Proc called within the RPG.

Have fun!

statements-set-transaction

control-example-using-transaction-logging-file-start-application

definition-example-jobs-commitment-definitions

╭ゆ眷念 2025-01-24 07:24:40

谢谢@Jairo,我已经成功了。

您的解决方案是正确的并且有效,唯一的挑战是 SP2 没有任何导致问题的 SQL 语句,我将 SP2 中的语句转换为 SQL 语句,并且它工作得很好。

Thank you @Jairo, I got it working.

Your solution was correct and it worked, only challenge was the SP2 didn't have any SQL statements which was causing issue, I converted the statements in SP2 to SQL statements and it worked completely fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文