0

Im trying to move data from one table to another because the needs of the project have changed. I figured a LOOP would work, but I keep getting errors (it annoys me how non-descript mysql error messages are).

Here's my code:

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;

This should create 2 rows in the reservationbody table for each row in the reservations table. I think the problem is with the loop, but I've built this query based on the mysql manual link at http://dev.mysql.com/doc/refman/5.0/en/loop-statement.html

Here's the error(s)

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

Can anyone tell me what's wrong?

Thanks in advance for the help!

1 Answer 1

2

You can do it simply and without loops, just execute INSERT...SELECT statements two times -

INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
  SELECT reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm FROM reservations;

INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
  SELECT reservationid, time_in, time_out, FromLocation, 'Home', startkm, endkm FROM reservations;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.