0

プロジェクトのニーズが変化したため、あるテーブルから別のテーブルにデータを移動しようとしています。LOOP が機能すると考えましたが、エラーが発生し続けます (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;

これにより、reservation テーブルの各行に対して、reservationbody テーブルに 2 つの行が作成されます。問題はループにあると思いますが、http: //dev.mysql.com/doc/refman/5.0/en/loop-statement.html の mysql マニュアル リンクに基づいてこのクエリを作成しました。

ここにエラーがあります

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
---------------------------------------------------

誰が何が悪いのか教えてもらえますか?

助けてくれてありがとう!

4

1 に答える 1