我在兽医诊所的数据库中创建了一个程序,该程序显示的是:第一次约会,第一次获得兽医授权执行所要求的服务(不得在同一时间预约其他约会)时间)。但是,当我调用此过程时,光标停止,因为eof的值为1,但它并没有真正看到所有记录:光标在行结束前离开循环!我不明白为什么。 这是代码:
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 :(得分: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抓住了他们。并留下光标。