简体   繁体   中英

mysql stored procedure always returning out parameter value = 0

MySql Stored Procedure

DELIMITER $$

USE `RMS`$$

DROP PROCEDURE IF EXISTS `posHdrTrxEntry`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `posHdrTrxEntry`(

IN mbrID INT,
IN usrID INT,
IN unqID VARCHAR(50),

OUT trxID INT
)
BEGIN
DECLARE trxID INT;

INSERT INTO `RMS`.`transaction`
(`MemberID`,`UserID`,`UID`)
VALUES (mbrID,usrID,unqID);

IF  ROW_COUNT() > 0 THEN
SET trxID=(SELECT ID FROM `transaction` WHERE `transaction`.`UID`=unqID);
END IF;

IF trxID > 0 THEN
UPDATE `transaction` SET `transaction`.`UID`=0 WHERE `transaction`.`ID`=trxID;
END IF;

END$$

DELIMITER ;

Java --Code Snippet

Connection connection = null;
    int trxID;
    try {
        connection = new DbService().getConnection();
 CallableStatement cs = connection.prepareCall("{ CALL posHdrTrxEntry(?,?,?,?) }");
        cs.setString(1, null);
        cs.setInt(2, item.getUserID());
        cs.setString(3, item.getUid());
        cs.registerOutParameter(4, java.sql.Types.INTEGER);
        cs.execute();
        trxID=cs.getInt(4);
        cs.close();

Stored Procedure always returning out parameter value = 0, even stored procedure last update statement execute successfully

IF trxID > 0 THEN
UPDATE `transaction` SET `transaction`.`UID`=0 WHERE `transaction`.`ID`=trxID;
END IF;

Its means that TrxID is greater than 0, why I get value only 0 in java?

Edit (Picture View) 在此输入图像描述

Dear just remove the first statement after BEIGN

Remove the Following:

DECLARE trxID INT;

Because this is out parameter , u can just use SET to assign the value.

I think It will work fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM