2015-12-13 56 views
0

我在獸醫診所的數據庫中創建了一個程序,顯示什麼是:第一次約會,第一次與授權執行所請求的服務的獸醫一起使用(不得有其他約會同時預訂)。但是當我調用這個過程時,光標會停止,因爲eof的值是1,但它並沒有真正看到所有的記錄:光標在行尾之前離開循環!我不明白爲什麼。 這裏是代碼:錯誤程序mysql db

DELIMITER // 

DROP PROCEDURE IF EXISTS first_booking_available // 

CREATE PROCEDURE first_booking_available(IN Perf VARCHAR(30), IN DateP DATE) 
BEGIN 
DECLARE IdV, eof INTEGER; 
DECLARE Dat DATE; 
DECLARE i, TimeB, End TIME; 

DECLARE vet CURSOR FOR E.Id_vet, W.Date, S.Start_time, S.End_time 
        FROM Enabled E, Work W, WorkShift S 
        WHERE E.Code_performance = Perf AND W.Id_vet = E.Id_vet AND S.Code = W.Code_workshift AND W.Date >= DateP ORDER BY W.Date, S.Start_time; 

DECLARE CONTINUE HANDLER FOR NOT FOUND SET eof = 1; 

SELECT Duration INTO @Duration FROM Perfomance WHERE Code = Perf; 

SET @Start_time = NULL; 

OPEN vet; 

read_loop: LOOP 

    FETCH vet INTO IdV, Dat, TimeB, End; 

    IF (eof = 1 OR @Start_time IS NOT NULL) THEN 

     LEAVE read_loop; 

    END IF; 

     SET i = TimeB; 

     WHILE i < End AND @Start_time IS NULL DO 

      SET @P = NULL; 
      SET @F = NULL; 

      SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO @P 
      FROM Booking B, Performance P 
      WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time = i; 

      SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO @F 
      FROM Booking B, Performance P 
      WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time BETWEEN i AND ADDTIME(i, @Duration) ORDER BY ADDTIME(B.Start_time, P.Duration) DESC LIMIT 1; 

      IF @P IS NOT NULL THEN 
       SET i = @P; 
      END IF; 

      IF @F IS NOT NULL THEN 
       SET i = @F; 
      END IF;    


      IF ADDTIME(i, @duration) > End THEN 
       SET i = End; 
      END IF; 

      IF ((@P IS NULL) AND (@F IS NULL) AND (i != End)) THEN 
       SET @Start_Time = i;  
      END IF; 

     END WHILE; 

     IF @Start_Time IS NOT NULL THEN 
      SET @Id_vet = IdV; 
      SET @Date = Dat; 
     END IF; 

END LOOP; 

CLOSE vet; 

IF @Start_Time IS NULL THEN 
    SIGNAL sqlstate '99994' 
    SET MESSAGE_TEXT = 'there is no availability !';  
END IF; 

END // 

DELIMITER ; 
+0

您使用的是什麼版本的MySQL? –

回答

0
DECLARE CONTINUE HANDLER FOR NOT FOUND SET eof = 1; 

是意味着你的查詢沒有結果。它不僅適用於光標,也適用於其他選擇查詢。

SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO @P 
     FROM Booking B, Performance P 
     WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time = i; 

     SELECT ADDTIME(ADDTIME(B.Start_time, P.Duration), '00:01:00') INTO @F 
     FROM Booking B, Performance P 
     WHERE P.Code = B.Code_performance AND B.Date = Dat AND B.Id_vet = IdV AND B.Start_time BETWEEN i AND ADDTIME(i, @Duration) ORDER BY ADDTIME(B.Start_time, P.Duration) DESC LIMIT 1; 

此查詢結果不存在。 HANDLER抓住他們。並離開光標。