Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
游标循环中的MySQL语法错误_Mysql_Loops_Stored Procedures_Cursor_Syntax Error - Fatal编程技术网

游标循环中的MySQL语法错误

游标循环中的MySQL语法错误,mysql,loops,stored-procedures,cursor,syntax-error,Mysql,Loops,Stored Procedures,Cursor,Syntax Error,我正在尝试使用以下脚本在存储过程中运行游标循环: CREATE PROCEDURE migrateMStruct() BEGIN DECLARE finished INT DEFAULT FALSE; DECLARE msId BIGINT(20) UNSIGNED; DECLARE luId INT(11); DECLARE msCursor CURSOR FOR SELECT id, live_unit_id FROM mounting_structures WHERE l

我正在尝试使用以下脚本在存储过程中运行游标循环:

CREATE PROCEDURE migrateMStruct()
BEGIN
DECLARE finished INT DEFAULT FALSE;
DECLARE msId BIGINT(20) UNSIGNED;
DECLARE luId INT(11);

DECLARE msCursor CURSOR FOR
    SELECT id, live_unit_id FROM mounting_structures
    WHERE live_unit_id IS NOT NULL;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = TRUE;

OPEN msCursor;

read_loop: LOOP
    FETCH msCursor INTO msId, luId;
    IF finished THEN
        LEAVE read_loop;
    END IF;
    UPDATE live_units SET mountingStructureId = msId WHERE live_units.id = luId;
END LOOP;

CLOSE msCursor;
END;
我根据MySQL文档编写了此脚本,但在执行脚本时收到错误:

SQL Error [1064] [42000]: 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 '' 
at line 19

第19行只是
结束IF语句。显然,这不是世界上最有用的错误,但根据MySQL的文档,语法看起来是正确的。

我也遇到了同样的错误,您必须临时更改分隔符,以防止在SQL解释器发现分号时执行

DELIMITER $$ 

CREATE PROCEDURE migrateMStruct()
BEGIN

  /*your procedure body*/

END$$

DELIMITER ;  /*back to previous delimiter*

为什么不使用一个简单的
UPDATE live\u units internal JOIN mounting\u structures ON live\u units.id=mounting\u structures.live\u unit\u id SET live\u units.mountingstructures id=mounting\u structures.id,其中mounting\u structures.live\u unit\u id不为NULL
??那么你就不需要使用光标了。这似乎可以完成任务,谢谢。我的同事似乎认为我需要使用光标。我仍然很好奇我的语法出了什么问题,但至少这是我需要的。请使用
分隔符
命令。我怀疑您的建议也会解决@wchiquito的问题