2013-02-14 57 views
2

如何在同一例程中使用兩個遊標?我在創建過程時遇到「遊標或處理程序聲明後的變量或條件聲明」錯誤。 我必須使用整數outerDone & 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; 
+0

如果可能,您是否可以爲您的問題創建SQL小提琴[http://sqlfiddle.com/](http://sqlfiddle.com/)?它只是使測試解決方案變得更容易。 – neelsg 2013-02-14 21:18:03

回答

0

您是否嘗試過你的光標移動之前的

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

報表?