1. 程式人生 > >mysql多個遊標儲存過程例子

mysql多個遊標儲存過程例子

功能:從personinfo 和 employee表中得到各自主鍵成為personConnect表的主鍵插入

CREATE PROCEDURE insertPersonConnet()
begin 
    declare personConnetId varchar(20);
    declare emplyeeConnetId varchar(20);
    declare flag int default 0;
    declare personConnetIds cursor for(select personId  from personinfo);
    declare employeeConnetIds cursor for(select employeeId  from employee);
    declare continue handler for not found set flag=1;
    open personConnetIds;
    person_loop:loop
            FETCH personConnetIds into personConnetId;
            if flag =1 then
                leave person_loop;
            end if;
            INSERT into personconnet(personId,persontype,isconnect,version) values(personConnetId,'系統人員','N',0);
    end loop person_loop;
    close personConnetIds;

    set flag = 0;
    open employeeConnetIds;
    employee_loop:loop
        FETCH employeeConnetIds into emplyeeConnetId;
        if flag =1 then
            leave employee_loop;
        end if;
        INSERT into personconnet(personId,persontype,isconnect,version) values(emplyeeConnetId,'員工人員','N',0);
    end loop employee_loop;
    close employeeConnetIds;
end

呼叫儲存過程:call  insertPersonConnet();