(2.14)Mysql之SQL基础——游标

时间:2023-03-09 09:01:40
(2.14)Mysql之SQL基础——游标

(2.14)Mysql之SQL基础——游标

关键词:Mysql游标

-- (1)定义游标
declare cur_name cursor for
select * from table_name where condition; -- (2)打开游标
open cur_name; -- (3)使用游标
fetch cur_name.column_name into 变量; -- 取下一行 -- (4)关闭游标
close cur_name; -- (5)演示

delimiter //
create procedure sp_test3(out result text)
begin
declare flag bit default 0;
declare temp varchar(30);
declare cur cursor for select b_name from bm;
declare continue handler for not found set flag = 1;
open cur;
while flag!=1 do
fetch cur into temp;
if flag!=1 then
set result = concat_ws(',',result,temp);
end if;
end while;
close cur;
end //