views:

30

answers:

1

Hello fellows, I am very new to stored procedures. I am trying to make a stored procedure which would perform an update accepting 2 input parameters and return the number of records updated (in my case always 1).

I have managed till this so far but I don't understand how to return the variable after opening the cursor. Is the cursor necessary at all in my case ? Please correct me if my approach is wrong.

    CREATE PROCEDURE PAKRETST.FTUMODINST (IN GENFORTREATYSRNO INTEGER,IN GENFTMDPDUENO INTEGER, OUT NUMREC SMALLINT)
RESULT SETS 0 MODIFIES SQL DATA LANGUAGE SQL
P1:BEGIN
    DELCARE strCmd VARCHAR(500);
    DECLARE x CURSOR WITH RETURN TO CALLER FOR SL;
    SET strCmd='UPDATE PAKRETST.UWFTMDPDUEDATES a where a.GENFORTREATYSRNO='||GENFORTREATYSRNO||'AND a.GENFTMDPDUENO='||GENFTMDPDUENO;
    PREPARE SL FROM strCmd;
    SET NUMREC= -- Stuck here --
    RETURN;
    END
    ;
+3  A: 

You don't need a cursor. Just use GET DIAGNOSTICS NUMREC = ROW_COUNT on the line you're stuck on.

More info here.

Thanks for the reply, it was pretty helpful. Do you know how to return the out parameter in the end of procedure ? Also is a commit necessary after update ?
Popo
After the procedure returns, the value of the OUT parameter will be available in the variable that you supplied for that parameter when you called the procedure.You COMMIT after an UPDATE that occurs within a transaction. Otherwise, COMMIT isn't necessary.
Thank you Jeff your reply was so helpful =)
Popo