MySQL游标获取空值

时间:2016-10-05 08:43:30

标签: mysql stored-procedures cursor

从游标获取的数据为NULL。但是我的查询select id from Projects返回的行数是正确的。我在我的过程循环中放了一个select语句来调试。它为id返回null。 我跟着这个[https://dev.mysql.com/doc/refman/5.5/en/cursors.html] 这段代码出了什么问题?

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `ReportDiffProDiffFinYear`()
BEGIN

DECLARE id INT;
DECLARE sqlstr varchar(10000) default "select financialYear as 'name' "; 
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR select id from Projects; /* My CURSOR QUERY*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;


 OPEN cur1;

 read_loop: LOOP
  FETCH cur1 INTO id;
  select id;
  IF done THEN
  LEAVE read_loop;
  END IF;
  set sqlstr = concat(sqlstr, ", sum(if(projects_id=",id,",0,1)) ");
  END LOOP;

  CLOSE cur1;
   set @sqlstr = concat(sqlstr," from (select * from WaterPoints  where status='Waterpoint Complete') as wp, (select id,financialYear from ProjectDetails) as pd where pd.id=wp.projectDetails_id group by  pd.financialYear");

 prepare stmt from @sqlstr;
 execute stmt;
 deallocate prepare stmt;
 END

1 个答案:

答案 0 :(得分:2)

我相信你的变量名id与游标的查询冲突,因此游标查询获取id变量的值(null),而不是{{ {1}}表。更改变量的名称或使用projects引用查询中的列。