DB2存储过程未返回结果集

时间:2015-01-08 10:44:00

标签: spring stored-procedures jdbc db2

我有一个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层执行上述存储过程时,不返回任何结果集。我需要对上面的存储过程进行哪些更改才能返回结果集?

2 个答案:

答案 0 :(得分:0)

你想要什么回来?

  • 有一个光标,您可以在其中选择元素数量,并将其指定给变量。之后,光标关闭。
  • 根据检索到的数量,执行插入。

我不知道你的期望:

  • 您想要返回结果集吗?然后你应该有一个打开的光标。 (例如,从USERBUSINESSAREA中选择*)
  • 是否要在光标中返回检索到的值?然后,正如@mustaccio所说,你可以添加一个OUT参数,然后只需赋值。

答案 1 :(得分:0)

以下修改过的存储过程对我有用(刚刚将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;