存储过程变量赋值不灵了?

fnzh001 2010-08-05 12:09:27
我的存储过程如下:
delimiter $$
create procedure sCode ()
begin
declare icode varchar(10);
declare cur_sCode cursor for (select symbol from quote group by symbol);
declare exit HANDLER for not found close cur_sCode;
open cur_sCode ;
repeat
fetch cur_sCode into icode;
create table icode (select * from quote where symbol=icode order by date desc);
until 0 end repeat;
close cur_sCode;
end;
$$
注意, create table icode (select * from quote where symbol=icode order by date desc);
这句话的意图是创建表,表名是变化的,icode取不同的值,创建不同的表,为何这个存储过程执行后,出现这样的问题
仅仅创建了一个表,表名就是icode?但是select * from quote where symbol=icode order by date desc这句话,执行的时候,确将icode的值正确赋值给了symbol,没有将icode这个字符串赋值给symbol,为何 create table icode没有将icode对应的值付给table名称,而是将icode赋值给table??
...全文
93 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixianxxx 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fnzh001 的回复:]
我自己通过测试解决了,
delimiter $$
create procedure sCode ()
begin
declare icode varchar(10);
declare cur_sCode cursor for (select symbol from quote group by symbol);
declare exit HANDLER for not ……
[/Quote]

拼接的时候处理单引号

delimiter $$
create procedure sCode ()
begin
declare icode varchar(10);
declare cur_sCode cursor for (select symbol from quote group by symbol);
declare exit HANDLER for not found close cur_sCode;
open cur_sCode ;
repeat
fetch cur_sCode into icode;
set @sql = concat('create table ',icode,'( select * from quote where symbol=''',icode,''' order by date desc);');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1;
until 0 end repeat;
close cur_sCode;
end;
$$
feixianxxx 2010-08-05
  • 打赏
  • 举报
回复
这个需要动态执行

否则的话 它不会吧icode当真变量的
fnzh001 2010-08-05
  • 打赏
  • 举报
回复
我自己通过测试解决了,
delimiter $$
create procedure sCode ()
begin
declare icode varchar(10);
declare cur_sCode cursor for (select symbol from quote group by symbol);
declare exit HANDLER for not found close cur_sCode;
open cur_sCode ;
repeat
fetch cur_sCode into icode;
set @sql = concat('create table ',icode,'( select * from quote where symbol=',''',icode,''',' order by date desc);');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1;
until 0 end repeat;
close cur_sCode;
end;
$$
ACMAIN_CHM 2010-08-05
  • 打赏
  • 举报
回复
ACMAIN_CHM 2010-08-05
  • 打赏
  • 举报
回复
delimiter $$
create procedure sCode ()
begin
declare icode varchar(10);
declare cur_sCode cursor for (select symbol from quote group by symbol);
declare exit HANDLER for not found close cur_sCode;
open cur_sCode ;
repeat
fetch cur_sCode into icode;
set @sql = concat('create table ',icode,' select * from quote where symbol=',icode,' order by date desc');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1;
until 0 end repeat;
close cur_sCode;
end;
$$
fnzh001 2010-08-05
  • 打赏
  • 举报
回复
mysql> call sCode();
ERROR 1054 (42S22): Unknown column 'AACC' in 'where clause'
这个小毛病如何解决?
fnzh001 2010-08-05
  • 打赏
  • 举报
回复
估计问题出现在这里,已经重新改写,还是不行
1. set @sql = concat('create table ',icode,'( select * from quote where symbol=',''',icode,''',' order by date desc);');
可以运行,产生了新的表名,但是每个表都是空的
2.set @sql = concat('create table ',icode,'( select * from quote where symbol='',icode,''order by date desc);');
可以运行,产生了新的表名,但是每个表都是空的
请问,如何处理?
fnzh001 2010-08-05
  • 打赏
  • 举报
回复
有问题,问题很大,打开所有的表发现都是空的。

56,678

社区成员

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

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