> 数据库 > MySQL >

MySQL存储过程游标示例

需要注意的是输出参数也是要初始化的,不然就是null值。
 
drop procedure cursor_user;
delimiter //
create procedure cursor_user(out result varchar(2000))
BEGIN
declare a varchar(20);
declare done int;
declare cur CURSOR FOR select id from test.`user`;
DECLARE CONTINUE HANDLER for not found SET done = 1;
open cur;
set result = '';
repeat
    FETCH cur into a;
    SELECT CONCAT(a,result) into result;
    #SELECT result;
until done end repeat;  
#SELECT result;
CLOSE cur;
END;
//
 
delimiter ;
 
/*启动游标遍历*/
set @a = 'hi';
#SELECT @a;
call cursor_user(@a);
select @a;
 


(责任编辑:IT)