我在 mysql 存储过程中遇到问题,其中 where 子句中的 varchar 变量不返回结果。下面给出了查询。
declare itcode varchar(30);
declare qty int;
declare finished int;
declare testc cursor for
select itemcode from mytable limit 0,10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
open testc;
read_loop:Loop
fetch testc into itcode;
if finished=1 then
leave read_loop;
end if;
select sum(Qty) as total from
mytable2
where itemcode=itcode;
end loop;
close testc;
在上面的语句中,即使两个表中都存在项目代码,它也会返回 null。但是,如果我在 close 上使用手动分配的值编写语句,如下所示,它会起作用。
select sum(Qty) as total from mytable2 where itemcode='p2343';
我无法弄清楚为什么 varchar 变量在 where 子句中不起作用。有人可以让我帮我弄清楚如何解决这种类型的问题吗?
注意:两个表列均为 varchar(30)。
附加说明:当我如下更改语句时,它也会打印 itcode 中的值。
select sum(Qty) as total,itcode from mytable2 where itemcode=itcode
因此 itcode 的值为“p2343”,但上述存储过程不起作用。
最佳答案
这里的问题是,该过程正在引用您的全局变量 qty
,以支持 mytable2
上的 Qty
列。尝试改变这个:
declare qty int;
到此
declare v_qty int;
关于Mysql存储过程在where子句中使用varchar变量的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23604016/