在mysql中用循环填充表

时间:2012-05-14 10:48:12

标签: mysql loops

我试图将数据从一个表移动到另一个表,因为项目的需求已经改变。 我认为一个LOOP会起作用,但我一直都会遇到错误(这让我很烦恼,我的错误消息是非描述性的。)

这是我的代码:

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;

这应该在预留表中为每个行创建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
---------------------------------------------------

谁能告诉我什么是错的?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

你可以简单而无循环地执行它,只需执行两次INSERT ... SELECT语句 -

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;