我对 SQL 相当陌生,所以如果我的代码和/或解释不清楚,我深表歉意,但如果有不清楚的地方,请询问我。
基本上我有一个过程,它使用两个游标根据某些条件将段值分配给 flight_times 表。在这些情况下,我将要么 1) 分配一个 segmentID,然后从段游标中获取下一条记录,2) 分配一个空值,然后从 flight_times 游标中获取下一行,或者 3) 如果航班日期和航班号不匹配,从段游标中获取下一行。
我遇到的问题是我不能让 IF 语句接受多个语句而不说我有语法错误。例如,在我尝试分配空值后,我无法从时间游标中获取另一条记录。对于我在这里做错的事情,任何人都可以提供任何帮助,我将不胜感激。我已经查看了 MySQL 手册,它确实表明我可以有多个语句
CREATE PROCEDURE write_flight_times(OUT f_date DATE, OUT f_no VARCHAR(20), OUT s_segid INT, OUT s_iataid CHAR(3),
OUT ft_date DATE, OUT ft_no VARCHAR(20), OUT ft_iataid CHAR(3))
BEGIN
DECLARE var_no_more_rows BOOLEAN DEFAULT FALSE;
DECLARE var_no_more_rows1 BOOLEAN DEFAULT FALSE;
DECLARE cur_segments CURSOR FOR
SELECT flightdate, flightno, segmentid, iataid
FROM flights f JOIN segments s ON f.flightid = s.flights_flightid
ORDER BY flightdate, flightno, segmentid;
DECLARE cur_times CURSOR FOR
SELECT flights_flightdate, flights_flightno, a.iataid
FROM temp_flight_times tft JOIN airports a ON a.icaoid = tft.airports_icaoid
ORDER BY flights_flightdate, flights_flightno, depttime;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET var_no_more_rows := TRUE;
SET var_no_more_rows1 := TRUE;
OPEN cur_segments;
loop_segments:
LOOP
FETCH cur_segments INTO f_date, f_no, s_segid, s_iataid;
IF var_no_more_rows THEN
CLOSE cur_segments;
LEAVE loop_segments;
END IF;
OPEN cur_times;
loop_times:
LOOP
FETCH cur_times INTO ft_date, ft_no, ft_iataid;
IF var_no_more_rows1 THEN
CLOSE cur_times;
LEAVE loop_times;
END IF;
IF (f_date = ft_date AND f_no = ft_no) THEN
IF s_iataid = ft_iataid THEN
s_segid;
ELSE
NULL;
FETCH cur_times INTO ft_date, ft_no, ft_iataid;
END IF
ELSE
FETCH cur_segments INTO f_date, f_no, s_segid, s_iataid;
END IF) AS segmentid
END LOOP loop_times;
END LOOP loop_segments;
END$$