1

I got the exception to create. Can anybody help me? How to write nested loop in stored procedure in Mysql the proper way and what is my wrong?

DELIMITER $$

`INVESTMENT_MATCH_POINT_CREATOR`(_percentage INT, _vat_tex INT)
BEGIN
    DECLARE _user_id INT; 
    DECLARE _package_id INT;


    DECLARE _left_investment INT;
    DECLARE _right_investment INT;

    DECLARE _left_point INT;
    DECLARE _right_point INT;

    DECLARE _left_carry_point INT;
    DECLARE _right_carry_point INT;

    DECLARE _get_point INT;

    DECLARE done BOOLEAN DEFAULT FALSE;
    DECLARE _user_investment_table CURSOR FOR SELECT user_id,package_id,left_invesetment,right_investment DATA FROM user_investment_match;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN _user_investment_table;

    read_loop:LOOP

    FETCH _user_investment_table INTO _user_id,_package_id,_left_investment,_right_investment;

    IF done THEN 
    LEAVE read_loop;
    END IF;

        BEGIN
                DECLARE _match_point INT; 
                DECLARE done2 BOOLEAN DEFAULT FALSE;

                DECLARE _package_match_point_table CURSOR FOR SELECT match_point DATA FROM package_match_points WHERE package_id=_package_id ORDER BY match_point DESC;
                DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;

                OPEN _package_match_point_table;
                read_loop2:LOOP

                FETCH _package_match_point_table INTO _match_point;

                    IF done2 THEN 
                    LEAVE read_loop2;
                    END IF;

                    /*if(_match_point=<_left_investment) and (_match_point=<_right_investment) then
                        set _left_point=abs(_left_investment-_match_point);
                        set _right_point=abs(_right_investment-_match_point);
                        set _get_point=((_match_point*_percentage)/100);
                    end if;*/

                END LOOP;
                CLOSE _package_match_point_table;
            END$$




    END LOOP;
    CLOSE _user_investment_table;

END$$

DELIMITER ;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I would wager that 99% of the time, if you're nesting cursors, you're doing something wrong. [You should look into set based solutions instead of procedural ones.](http://stackoverflow.com/questions/1687512/rbar-vs-set-based-programming-for-sql) – Michael Fredrickson Mar 06 '12 at 19:49
  • Your sample code is too long. – usr Mar 06 '12 at 20:10

1 Answers1

1

You should use the specified delimiter only at the end of the procedure, however in your code you are using the $$ delimiter at the end of the inner block, (and in other words you are using the $$ delimiter which signals the end of the procedure before it is actually ended)

yoel halb
  • 12,188
  • 3
  • 57
  • 52