我的光标程序不起作用

时间:2013-01-13 11:59:02

标签: mysql stored-procedures

我有下一个程序:

BEGIN
  DECLARE retribAn INTEGER DEFAULT 0; 
  DECLARE cPost INTEGER(11);     
  DECLARE done INT DEFAULT 0;
  DECLARE curTipo CURSOR FOR
          SELECT RETRIBUCION_ANUAL*1.05 AS RET_AN
          FROM EMPLEADOS  
          WHERE ID_CPOSTAL%2=0;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
  OPEN curTipo;
  REPEAT
    FETCH curTipo INTO retribAn, cPost;
    IF NOT done THEN       
      UPDATE EMPLEADOS 
      SET RETRIBUCION_ANUAL=retribAn  
      WHERE ID_CPOSTAL%cPost;  
    END IF;
  UNTIL done END REPEAT;
  CLOSE curTipo;    
END

当我执行它时,sql manager输出:

  

FETCH变量数量不正确

我不明白为什么。

我是mysql中的菜鸟= D,对不起我的英语。

由于

1 个答案:

答案 0 :(得分:3)

您只在光标查询中选择一列并将数据提取到FETCH中的两个变量

试试这个

BEGIN
  DECLARE retribAn INTEGER DEFAULT 0; 
  DECLARE cPost INTEGER(11);     
  DECLARE done INT DEFAULT 0;
  DECLARE curTipo CURSOR FOR
          SELECT RETRIBUCION_ANUAL*1.05 AS RET_AN
          FROM EMPLEADOS  
          WHERE ID_CPOSTAL%2=0;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
  OPEN curTipo;
  REPEAT
    FETCH curTipo INTO retribAn; --remove cPost here
    IF NOT done THEN       
      UPDATE EMPLEADOS 
      SET RETRIBUCION_ANUAL=retribAn  
      WHERE ID_CPOSTAL%cPost = 0;  --change this to value you want to check
    END IF;
  UNTIL done END REPEAT;
  CLOSE curTipo;    
END