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

mysql 存储过程请教
我想实现这样的功能,用SQL语句(是select语句,后面的where条件是变的)作为参数传入,执行该参数的sql语句;然后根据select的结果,逐条插入另外一张表,现在的问题的是?我不知道用什么方式执行该参数的sql语句,执行的时侯要能返回每条记录的select值,然后将该值用于insert语句的插入值?

------解决方案--------------------
有可能是没有满足条件的记录,检查一下
------解决方案--------------------
CREATE DEFINER=`root`@`%` PROCEDURE `sp_generate_auto`(in strsql varchar(200) character set gbk,in icount int,in hfzt varchar(200) character set gbk)
BEGIN
declare cnt int default 0;
declare i int default 0;
while i < icount
do
set @stmt = concat('select id into @c_id from customer where ',strsql,' order by id asc limit ',i,',1');
select 1,@stmt;
prepare s1 from @stmt;

execute s1;
select 2,@c_id;
deallocate prepare s1; 
set @stmt = NULL;
set @num = 0; 
set @stmt2 = concat('insert into hfztjl (hfzt,hfkhID) values ("',hfzt,'",',@c_id,')');
select 3,@stmt2;
select 4,@c_id;
prepare s1 from @stmt2;
execute s1; 
deallocate prepare s1; 
set @stmt2 = NULL; 
set i = i + 1;
set @c_id = NULL;
end while;
  
END;

贴一下结果