Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:13:04+00:00 2026-06-03T23:13:04+00:00

Im trying to move data from one table to another because the needs of

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:13:05+00:00Added an answer on June 3, 2026 at 11:13 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to move the data from one table to another based on names
I am trying to take the data from one table and move it over
I'm trying to move a control from one parent to another (if this will
I am trying to move dumped data from postgresql database to another. However, when
I am trying to move a WordPress installation from one server to another one,
I have one table which display data as from Dynamic Content in 1 column.
I am trying to move an object from one database into another. The mappings
I am trying to using a SQL data reader to move records from one
I'm wondering if it's possible to move all data from one column in table
I have some troubles trying to move data from SQL Server 2000 (SP4) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.