MySQL存储过程无法正常工作嵌套游标

时间:2019-05-14 09:27:32

标签: mysql stored-procedures

请注意,第一个游标读取3个结果,而当我插入时仅插入1个结果。更早的时候,我没有打开curs2时,它运行良好,但现在却没有。在很长一段时间后我正在编写存储过程时,我可能已经错过了done和done2变量。请查看您是否发现任何错误。 谢谢


BLOCK1: begin

    declare done INT DEFAULT FALSE;
    declare gid BIGINT;
    declare activity_name VARCHAR(255) ;
    declare type_1 VARCHAR(255) ;
    declare shop_activity_id BIGINT;
    declare curs CURSOR for select id, activityName, `Type`, shopActivityId from gantttask where `Type` = 'Project';

    open curs;
        LOOP1: loop
        fetch curs into gid, activity_name, type_1, shop_activity_id;
        if done then
            leave LOOP1;
        end if;
        insert into gantttask_result VALUES (gid, activity_name, type_1, shop_activity_id);

        BLOCK2: begin
            declare done2 INT DEFAULT FALSE;
            declare gid2 BIGINT;
            declare activity_name2 VARCHAR(255) ;
            declare type_2 VARCHAR(255) ;
            declare shop_activity_id2 BIGINT;
            declare curs2 CURSOR for select id, activityName, `Type`, shopActivityId from gantttask where `Type` != 'Project' and shopActivityId = shop_activity_id order by id;

            open curs2;
                LOOP2: loop
                fetch curs2 into gid2, activity_name2, type_2, shop_activity_id2;
                if done2 then
                    leave LOOP2;
                end if;

                end loop LOOP2;
            close curs2;
        end BLOCK2;

        end loop LOOP1;
    close curs;
end BLOCK1

0 个答案:

没有答案