存储过程动态取值和赋值问题

gongjieqi 2012-12-05 11:52:04
代码如下,运行时 显示我变量b_column_value未定义
ERROR 1327 : Undeclared variable: b_column_value

t_id w_id bname 是从外面传入的
begin
/*基础变量声明*/
DECLARE u_id int;
DECLARE r_id int;
DECLARE t_year int;

DECLARE b_name varchar(255);
DECLARE b_baifen decimal(10,2);
DECLARE b_quan decimal(10,6);
DECLARE b_value int;
DECLARE b_min decimal(10,4);
DECLARE b_max decimal(10,4);
DECLARE b_column_name varchar(255);
DECLARE b_table_name varchar(255);
DECLARE b_column_value decimal(10,4);

DECLARE result decimal(10,6) default 0.000000;
DECLARE done int(10);
DECLARE cursor_name CURSOR FOR select bin_baifen,bin_quan,bin_value,min_value,max_value,bin_column_name,bin_table_name from jbfx_yinsu where bin_name=bname;
DECLARE continue handler for not found set done=1;
open cursor_name;
loop_label:LOOP
fetch cursor_name into b_baifen,b_quan,b_value,b_min,b_max,b_column_name,b_table_name;
if done =1 then
leave loop_label;
else
set @str = concat('select ',b_column_name,' into b_column_value from ',b_table_name,' where wenjuan_id =',w_id,' and tijian_id = ',t_id);
prepare stmt1 from @str;
execute stmt1;
deallocate prepare stmt1;
if b_value != null then
if b_value = b_column_value then
set result = result+b_baifen*b_quan;
end if;
elseif (b_min != null) AND (b_max != null) then
if (b_column_value>= b_min) AND (b_column_value < b_max) then
set result = result+b_baifen*b_quan;
end if;
end if;
end if;
END LOOP;
close cursor_name;
select result;
end




...全文
210 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2012-12-05
  • 打赏
  • 举报
回复
改所有的 b_column_value 为 @b_column_value set @str = concat('select ',b_column_name,' into @b_column_value from ',b_table_name,' where wenjuan_id =',w_id,' and tijian_id = ',t_id); if b_value = @b_column_value then .... 因为你的PREPARE执行的SQL语句并不是在存储过程的变量空间内。
gongjieqi 2012-12-05
  • 打赏
  • 举报
回复
自行解决了 - -

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧