学习mysql存储过程,并且用到游标
时间:2014-05-14 07:41:31
收藏:0
阅读:361
定义一个存储过程,用到游标,从一个表中取值,插入到另外一个表中。
drop procedure if exists
search_test;
create procedure search_test(in id int,out out_min_id
varchar(200))
begin
declare finished boolean default 0 ;
declare tmp
varchar(200) ;
declare s_test cursor for select r_id from A where
n_code=id; //定义游标
declare continue handler for sqlstate ‘02000‘ set
finished=1; //定义结束标志
open
s_test;
//打开游标
repeat
//循环
FETCH s_test into tmp;
if not finished then
set
out_min_id=tmp;
insert into B(n_code) values(out_min_id);
end
if;
until finished end repeat;
close s_test;
//关闭游标
select out_min_id;
end
评论(0)