On Monday, March 26, 2012 5:10:19 PM UTC-5, CRPence wrote:
Taken a heckuva long time to get to this point, but here is where I'm at now. I think this is pretty much done for me.
CREATE PROCEDURE BEAK/GetNextTransactionNumber(
IN startCode CHAR(10),
IN defaultTrans CHAR(10),
OUT transactionNumber CHAR(10),
OUT O_SQLCODE INTEGER,
OUT O_SQLSTATE CHAR(5))
LANGUAGE SQL
BEGIN
/* General variable and condition declarations. */
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE DBGOUT VARCHAR(150);
DECLARE overflow CONDITION FOR SQLSTATE '22003';
/* Cursor declaration(s) */
DECLARE CURS1 CURSOR FOR SELECT TRTRANS FROM DFTRANMSTP WHERE TRSCODE = startCode FOR UPDATE OF TRTRANS;
/* Handler and condition declaration(s) */
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET O_SQLCODE = SQLCODE;
SET O_SQLSTATE = SQLSTATE;
set DBGOUT='General SQLException: SQLCODE=' concat SQLCODE concat ', SQLSTATE:' concat SQLSTATE;
CALL REXSAY(DBGOUT);
END;
DECLARE CONTINUE HANDLER FOR overflow
BEGIN
set DBGOUT='OVERFLOW CONDITION! SQLCODE = ' concat SQLCODE
concat ', SQLSTATE = ' concat SQLSTATE concat
': attempting UPDATE DFTRANMSTP SET TRTRANS = 1 WHERE CURRENT OF CURS1';
CALL REXSAY(DBGOUT);
UPDATE DFTRANMSTP SET TRTRANS = 1 WHERE CURRENT OF CURS1;
CALL REXSAY('Overflow for DFTRANMSTP recovered.');
END;
/* Now onto the actual code */
OPEN CURS1;
FETCH CURS1 INTO transactionNumber;
if SQLCODE <> 0 then
INSERT INTO DFTRANMSTP (TRTRANS, TRSCODE) VALUES (defaultTrans + 1, startCode);
set transactionNumber = defaultTrans;
else
/* If TRTRANS = 9,999,999,999 then executing this statement will cause the 'overflow' exception to trip. */
UPDATE DFTRANMSTP SET TRTRANS = TRTRANS + 1 WHERE CURRENT OF CURS1;