0
我試圖將數據從一個表格移動到另一個表格,因爲項目的需求已經改變。 我想LOOP會工作,但我不斷收到錯誤(它讓我很煩惱非mysql描述的非錯誤消息)。用mysql中的循環填充表格
這裏是我的代碼:
SET @resid := 0;
SET @total := (SELECT COUNT(*) FROM reservations) + 1;
BEGIN
label1: LOOP
IF @resid < @total THEN
SET @resid = @resid + 1
INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
VALUES(
(SELECT
reservationid,
time_in,
time_out,
ToLocation,
FromLocation,
startkm,
endkm
FROM reservations WHERE reservationid = @resid)
);
INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
VALUES(
(SELECT
reservationid,
time_in,
time_out,
FromLocation,
'Home',
startkm,
endkm
FROM reservations WHERE reservationid = @resid)
);
END IF;
LEAVE lable1;
END LOOP
END;
這應該創建一個在reservationbody表2列在預訂表的每一行。我認爲這個問題是與循環,但我已經建立了基於MySQL手冊鏈接,這個查詢在http://dev.mysql.com/doc/refman/5.0/en/loop-statement.html
這裏的錯誤(或多個)
0 ROW(s) affected
Execution TIME : 0.035 sec
Transfer TIME : 0.002 sec
Total TIME : 0.038 sec
---------------------------------------------------
0 ROW(s) affected
Execution TIME : 0.037 sec
Transfer TIME : 0.001 sec
Total TIME : 0.039 sec
---------------------------------------------------
QUERY: BEGIN label1: LOOP IF @resid < @total THEN SET @resid = @resid + 1 INSERT INTO reservationbody(reservationid, time_in, time_out...
Error CODE: 1064
You have an error IN your SQL syntax; CHECK the manual that corresponds TO your MySQL SERVER VERSION FOR the RIGHT syntax TO USE near 'label1: Loop
if @resid < @total then
set @resid = @resid + 1
INSERT INTO' AT line 2
Execution TIME : 0 sec
Transfer TIME : 0 sec
Total TIME : 0.039 sec
---------------------------------------------------
QUERY: INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm) VALUES((SELECT reserva...
Error CODE: 1136
COLUMN COUNT doesn't match value count at row 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.040 sec
---------------------------------------------------
Query: end if
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.039 sec
---------------------------------------------------
Query: leave lable1
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEAVE lable1' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.038 sec
---------------------------------------------------
Query: end loop end
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END LOOP
END' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.035 sec
---------------------------------------------------
誰能告訴我有什麼不對?
在此先感謝您的幫助!