我對SQL相當陌生,所以如果我的代碼和/或解釋不清楚,請致信我澄清,如果有些事情不清楚,我請道歉。IF,THEN語句在MySQL中有多個語句
基本上我有一個程序,它使用兩個遊標根據一些條件將一個分段值賦值給一個flight_times表。在這些條件下,我將1)分配一個segmentID,然後從段遊標中獲取下一條記錄,2)分配一個空值,然後從flight_times光標讀取下一行,或者3)如果flightdate和flight number不匹配,從段遊標中獲取下一行。
我遇到的問題是我不能讓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$$
您提供的代碼是否給出錯誤? – wallyk 2012-04-16 00:46:54
對不起,錯誤1064語法錯誤。它是在if語句中的分號之後的s_segid – Jason 2012-04-16 00:53:52