drop procedure proc_cursor;
create procedure proc_cursor(in one int,in sum int)
begin
DECLARE cursor_id int;
DECLARE cursor_na varchar(255);
DECLARE cursor_addr varchar(255);
DECLARE sb int default 0;
DECLARE a int default 0;
DECLARE b int default 0;
-- limit 下标从0 开始
set sb = (one - 1) * sum;
if one > 0 then
-- 游标前面的赋值sql语句会导致语法报错,解决方法用代码块把声明游标包起来
begin
-- 声明游标
DECLARE cursor_name CURSOR FOR select id,name,addr from student limit sb,sum;
DECLARE CONTINUE HANDLER FOR NOT FOUND set a = 1; -- 当读到数据的最后一条时,设置no变量为1
-- 打开游标 open
open cursor_name;
while a = 0 do
-- fetch是获取游标当前指向的数据行,并将指针指向下一行
fetch cursor_name into cursor_id,cursor_na,cursor_addr;
select CONCAT(cursor_id,cursor_na,cursor_addr);
end while;
-- 关闭游标 close
close cursor_name;
END;
else
select '输入错误';
end if;
END
call proc_cursor(1,10)
```