在存储过程中处理多个嵌套游标

时间:2013-02-14 20:55:40

标签: mysql stored-procedures

如何在同一例程中使用两个游标?我在创建过程时收到“游标或处理程序声明后的变量或条件声明”错误。 我必须使用整数outerDone和amp; innerDone来检查游标是否指向null。 我还没有处理存储过程。请问有人可以解决这个问题。在此先感谢!!

DELIMITER ##;
create procedure updateStopTimeColumn()
BEGIN
DECLARE outerDone INT DEFAULT 0;
DECLARE vehicle_record CURSOR FOR SELECT `vehicleId` FROM `vehicle`;
DECLARE current_record CURSOR FOR SELECT `id`,`tsTime`,`teTime` FROM `trip` where `vehicleId`=vehId order by `tsTime`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET outerDone = 1;
DECLARE vehId,tripId CHAR(250);
DECLARE currentTsTime,currentTeTime time;

OPEN vehicle_record;
REPEAT  
    FETCH vehicle_record INTO vehId;


block2 : BEGIN
    DECLARE innerDone INT DEFAULT 0;
    DECLARE tempTripId CHAR(250);
    DECLARE tempTsTime,tempTeTime time;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET innerDone = 1;

    OPEN current_record;

    FETCH current_record INTO tempTripId, tempTsTime,tempTeTime;

    REPEAT
        FETCH current_record INTO tripId,currentTsTime,currentTeTime;
        UPDATE trip set stopTime=(currentTeTime-tempTsTime) where id=tempTripId and tempTeTime IS NOT NULL;

        SET tempTripId=tempId;
        SET tempTsTime=currentTsTime;
        SET tempTeTime=currentTeTime;

    UNTIL innerDone END REPEAT;
END block2;
CLOSE current_record;
UNTIL outerDone END REPEAT;

CLOSE vehicle_record;
END; ##
DELIMITER;

1 个答案:

答案 0 :(得分:0)

您是否尝试过移动

DECLARE vehId,tripId CHAR(250);
DECLARE currentTsTime,currentTeTime time;

在你的游标之前的陈述?