DB2 存储过程不返回结果集

DB2 stored procedure is not returning the result set

我有一个 DB2 存储过程,它在数据库中插入一行。存储过程如下:

CREATE PROCEDURE ZSPPQINSERTUSERIDBA ( 
        IN P_USERID             CHAR(10),
        IN P_BUSINESSAREA       CHAR(10),
        IN P_SELECTEDIND        CHAR(1),
        IN P_DEFAULTIND         CHAR(1),
        IN P_LEGACYSYSTEM       CHAR(10),
        IN P_LEGACYLOGIN        CHAR(16),
        IN P_LEGACYPASSWORD     CHAR(16),
        IN P_OTHERLOGIN         CHAR(10),
        IN P_OTHERPASSWORD      CHAR(10),
        IN P_ADDSECURLOGIN      CHAR(10),
        IN P_ADDSECURPASSWORD   CHAR(10),
        IN P_LASTUPDATEUSERID   CHAR(10),
        IN P_LASTUPDATE         TIMESTAMP)

    DYNAMIC RESULT SETS 1 
    LANGUAGE SQL 
    SPECIFIC ZSPPQINSERTUSERIDBA 
    NOT DETERMINISTIC 
    MODIFIES SQL DATA 
    CALLED ON NULL INPUT
    SET OPTION  ALWBLK = *ALLREAD ,
    ALWCPYDTA = *OPTIMIZE , 
    COMMIT = *CS , 
    CLOSQLCSR = *ENDMOD ,
    DECRESULT = (31, 31, 00) , 
    DFTRDBCOL = *NONE , 
    DYNDFTCOL = *NO , 
    DYNUSRPRF = *USER ,
    RDBCNNMTH = *RUW ,
    SRTSEQ = *HEX 

    P1 : BEGIN

    DECLARE TOTALCNT INTEGER DEFAULT 0 ; 

    DECLARE C1 CURSOR WITH RETURN FOR 
      SELECT Count (*) FROM USERBUSINESSAREA
       WHERE USERID            = P_USERID
         AND BUSINESSAREA = P_BUSINESSAREA;

    OPEN C1;
     FETCH C1 INTO TOTALCNT;
    CLOSE C1;

    IF ( TOTALCNT = 0) THEN 
      INSERT 
      INTO  USERBUSINESSAREA    
        (
            USERID,
            BUSINESSAREA,
            SELECTEDIND,
            DEFAULTIND,
            LEGACYSYSTEM,
            LEGACYLOGIN,
            LEGACYPASSWORD,
            OTHERLOGIN,
            OTHERPASSWORD,
            ADDSECURLOGIN,
            ADDSECURPASSWORD,
            LASTUPDATEUSERID,
            LASTUPDATE
        )
            VALUES  (P_USERID,
                     P_BUSINESSAREA,
                     P_SELECTEDIND,
                     P_DEFAULTIND,
                     P_LEGACYSYSTEM,
                     P_LEGACYLOGIN,
                     P_LEGACYPASSWORD,
                     P_OTHERLOGIN,
                     P_OTHERPASSWORD,
                     P_ADDSECURLOGIN,
                     P_ADDSECURPASSWORD,
                     P_LASTUPDATEUSERID,
                     P_LASTUPDATE);
      END IF;
    END P1;

当我尝试从 Java/Spring-Integration 层执行上述存储过程时,return 没有结果集。上面的存储过程要return一个结果集需要做哪些改动?

你到底想要什么return?

  • 有一个游标,您可以在其中 select 元素的数量,并将其分配给一个变量。之后,游标关闭。
  • 根据检索到的数量,您执行插入。

不知道你期待什么:

  • 你想要return一个结果集吗?然后你应该有一个打开的游标。 (例如 select * 来自 USERBUSINESSAREA)
  • 是否要 return 游标中的检索值?然后,正如@mustaccio所说,你可以添加一个OUT参数,然后赋值即可。

以下修改后的存储过程对我有用(刚刚将 WITH RETURN FOR 更改为 RETURN TO CALLER FOR):

CREATE PROCEDURE ZSPPQINSERTUSERIDBA ( 
        IN P_USERID             CHAR(10),
        IN P_BUSINESSAREA       CHAR(10),
        IN P_SELECTEDIND        CHAR(1),
        IN P_DEFAULTIND         CHAR(1),
        IN P_LEGACYSYSTEM       CHAR(10),
        IN P_LEGACYLOGIN        CHAR(16),
        IN P_LEGACYPASSWORD     CHAR(16),
        IN P_OTHERLOGIN         CHAR(10),
        IN P_OTHERPASSWORD      CHAR(10),
        IN P_ADDSECURLOGIN      CHAR(10),
        IN P_ADDSECURPASSWORD   CHAR(10),
        IN P_LASTUPDATEUSERID   CHAR(10),
        IN P_LASTUPDATE         TIMESTAMP)

    DYNAMIC RESULT SETS 1 
    LANGUAGE SQL 
    SPECIFIC ZSPPQINSERTUSERIDBA 
    NOT DETERMINISTIC 
    MODIFIES SQL DATA 
    CALLED ON NULL INPUT
    SET OPTION  ALWBLK = *ALLREAD ,
    ALWCPYDTA = *OPTIMIZE , 
    COMMIT = *CS , 
    CLOSQLCSR = *ENDMOD ,
    DECRESULT = (31, 31, 00) , 
    DFTRDBCOL = *NONE , 
    DYNDFTCOL = *NO , 
    DYNUSRPRF = *USER ,
    RDBCNNMTH = *RUW ,
    SRTSEQ = *HEX 

    P1 : BEGIN

    DECLARE TOTALCNT INTEGER DEFAULT 0 ; 

    DECLARE C1 CURSOR WITH RETURN TO CALLER FOR
      SELECT Count (*)  FROM USERBUSINESSAREA
       WHERE USERID            = P_USERID
         AND BUSINESSAREA = P_BUSINESSAREA;

    OPEN C1;

     FETCH C1 INTO TOTALCNT;


    IF ( TOTALCNT = 0) THEN 
      INSERT 
      INTO  USERBUSINESSAREA    
        (
            USERID,
            BUSINESSAREA,
            SELECTEDIND,
            DEFAULTIND,
            LEGACYSYSTEM,
            LEGACYLOGIN,
            LEGACYPASSWORD,
            OTHERLOGIN,
            OTHERPASSWORD,
            ADDSECURLOGIN,
            ADDSECURPASSWORD,
            LASTUPDATEUSERID,
            LASTUPDATE
        )
            VALUES  (P_USERID,
                     P_BUSINESSAREA,
                     P_SELECTEDIND,
                     P_DEFAULTIND,
                     P_LEGACYSYSTEM,
                     P_LEGACYLOGIN,
                     P_LEGACYPASSWORD,
                     P_OTHERLOGIN,
                     P_OTHERPASSWORD,
                     P_ADDSECURLOGIN,
                     P_ADDSECURPASSWORD,
                     P_LASTUPDATEUSERID,
                     P_LASTUPDATE);
      END IF;

    END P1;