1

这个简单的存储过程应该获取未更新的行,并将它们插入到另一个表中。但是这种行为方式相当奇怪。

如果我要像下面那样运行以下过程,我会得到:Error 1329 No data - zero rows fetched, selected, or processed,但行会被插入。

样本表数据

"id"    "total" "vals"  "valLen"    "updated"
"1"     "0"     "0"     "0"         "1"
"2"     "0"     "1"     "1"         "0"
"3"     "0"     "1,2"   "2"         "0"
"4"     "0"     "1,2,3" "3"         "0"

注意 fetch 的位置

BEGIN
  #declare variable
  DECLARE procId, procTotal, proc INT(10);
  DECLARE procUpdated TINYINT(1);
  DECLARE procVals, procValLen VARCHAR(50);
  DECLARE done INT DEFAULT 0;
  DECLARE counter TINYINT(1);

  #declare cursor
  DECLARE cur1 CURSOR FOR SELECT id, total, vals, valLen, updated
  FROM scores
  WHERE updated = 0;

  #declare handle 
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  #open cursor
  OPEN cur1;

  #starts the loop
  the_loop: LOOP

 FETCH cur1 INTO procId, procTotal, procVals, procValLen, procUpdated;

    IF done THEN
      LEAVE the_loop;
    END IF;

     insert into scores (total) values (procId);

  END LOOP the_loop;

  CLOSE cur1;
END

如果我要运行如下所示的过程,我会插入 1 个额外的行。注意位置的变化FETCH

样本表数据

"id"    "total" "vals"  "valLen"    "updated"
"1"     "0"     "0"     "0"         "1"
"2"     "0"     "1"     "1"         "0"
"3"     "0"     "1,2"   "2"         "0"
"4"     "0"     "1,2,3" "3"         "0"

运行以下过程后

"id"    "total" "vals"  "valLen"    "updated"
"1"     "0"     "0"     "0"         "1"
"2"     "0"     "1"     "1"         "0"
"3"     "0"     "1,2"   "2"         "0"
"4"     "0"     "1,2,3" "3"         "0"
""      "4"     "0" "0"         "0"
""      "4"     "0" "0"         "0"
""      "3"     "0" "0"         "0"
""      "2"     "0" "0"         "0"
""      "1"     "0" "0"         "0"

注意 fetch 的位置

BEGIN
  #declare variable
  DECLARE procId, procTotal, proc INT(10);
  DECLARE procUpdated TINYINT(1);
  DECLARE procVals, procValLen VARCHAR(50);
  DECLARE done INT DEFAULT 0;
  DECLARE counter TINYINT(1);

  #declare cursor
  DECLARE cur1 CURSOR FOR SELECT id, total, vals, valLen, updated
  FROM scores
  WHERE updated = 0;

  #declare handle 
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  #open cursor
  OPEN cur1;

  #starts the loop
  the_loop: LOOP



    IF done THEN
      LEAVE the_loop;
    END IF;

FETCH cur1 INTO procId, procTotal, procVals, procValLen, procUpdated;

     insert into scores (total) values (procId);

  END LOOP the_loop;

  CLOSE cur1;
END

解决此问题并使其正常运行的方法是什么。

4

0 回答 0