存储过程不会更新MySQL

时间:2018-06-08 10:43:01

标签: mysql sql stored-procedures heidisql database-cursor

在HeidiSQL上运行此例程并且它受到影响的0行运行,即使应该正好影响1行。这两个select语句似乎在此存储过程之外正常工作。

BEGIN
    DECLARE someId INT;
    DECLARE done INT DEFAULT FALSE;
    DECLARE cur1 CURSOR FOR 
    select anotherId from tableA 
     where yetAnotherId IN(another select statement);
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;     

OPEN cur1;

    read_loop: LOOP

    IF done THEN
      LEAVE read_loop;
    END IF;

    FETCH cur1 INTO someId;

    update tableB
    set x = 'hello', y = 'world'
    where something = someId;

    END LOOP;

    CLOSE cur1;
END;

我想知道这个例程的结构可能有什么问题。在我看来,即使光标应该包含1个条目,它也不会。

由于

编辑:看起来'someId'匹配一个具有相同名称的表字段,因此问题。这已经解决了。

1 个答案:

答案 0 :(得分:0)

您的SP看起来不错,我已经为MySql做了更改,请尝试一下

BEGIN
DECLARE someId INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR 
select anotherId from tableA 
where yetAnotherId IN(another select statement);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;     

OPEN cur1;

read_loop: LOOP

FETCH cur1 INTO someId;

update tableB
set x = 'hello', y = 'world'
where something = someId;

IF done THEN

CLOSE cur1;
  LEAVE read_loop;

END IF;

END LOOP read_loop;


END;