日期:2014-05-16  浏览次数:20867 次

游标的输出怎么做为表名的变量?
游标的输出也是一种变量,我怎么不能用来做表名的变量?

use st;
DELIMITER $$
CREATE PROCEDURE 2in1()
begin
declare bm char(8);
declare found boolean default true;
declare bm_ cursor for select table_name from information_schema.tables where table_schema='st' limit 1,15000;
declare continue handler for not found set found=false;
open bm_;

while found do
fetch bm_ into bm;
if found=true then
begin
declare rq_ date;
declare found2 boolean default true;
declare rq_bcf cursor for select distinct(rq) from bm;
declare continue handler for not found set found2=false;
open rq_bcf;
while found2 do
fetch rq_bcf into rq_;

end while;
close rq_bcf;
end;
end if;
end while;

close bm_;
end$$
delimiter ;



调用时提示:
mysql> call 2in1;
ERROR 1146 (42S02): Table 'st.bm' doesn't exist

‘bm’怎么不能做表名的变量?

如何修改?
谢谢

------解决方案--------------------
MYSQL不支持表名,列名为变量。 你可以使用 prepare execute 来执行,可以参考手册中的例子。