mysql游标获取空值

5ktev3wc  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(374)

从游标获取的数据为空。但我查询的行数 select id from Projects 返回是正确的。我在我的过程循环中放了一个select语句进行调试。id返回空值。我跟踪了这个[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
8yparm6h

8yparm6h1#

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

相关问题