cursor游标(mysql)

/*
游标 cursor
什么是游标?为何须要游标
使用存储过程对sql进行编程的时候,咱们查询的语句多是数据是多个,它老是一口气所有执行,咱们没法针对每一条进行判断。也就是说,咱们没法控制程序的运行,因此引入了游标cursor
cursor相似于java中的迭代器。  它利用查询语句生成一个游标,而后游标中有一个相似指针的东西。首先指在游标首,就是迭代器。不解释了

   cursor 游标
   declare声明;  declare 游标名 cursor for select_statement;
   open 打开; open游标名
   fetch 取值; fetch 游标名 into var1,var2[,...]   select语句中查出的项有多少,就须要使用多少变量接受
   close 关闭; close 游标名                    
*/

create table goods
(
 id int,
 name varchar(20),
 num int
);
insert into goods values (1,'dog',20),(2,'cat',30),(3,'pig',25);
select * from goods;

-- 游标在存储过程当中使用

drop procedure p1;
create procedure p1()
begin
    
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量
      
     open gs;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     close gs;
     
end;

call p1();

create procedure p2()
begin
    
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量
      
     open gs;
     fetch gs into row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     close gs;
     
end;

call p2(); --报错,若是取出游标数据的个数超过游标中数据的个数,报错。相似于数组越界

drop procedure p3;
create procedure p3()
begin
    
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量
      
     open gs;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     close gs;
     
end;
call p3();

--学会使用循环控制试试
create procedure p4()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare count_r int;
     declare i int default 0;
    
     declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
     select count(*) into count_r from goods;
      
     open gs;
     repeat
          fetch gs into row_id,row_name,row_num;
          select row_id,row_name,row_num; 
          set i := i+1;
     until i>=count_r end repeat;
     close gs;
end;

call p4();  

-- 用while循环试试
create procedure p5()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare count_r int;
     declare i int default 0;
    
     declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
     select count(*) into count_r from goods;
      
     open gs;
     while i<count_r do
          fetch gs into row_id,row_name,row_num;
          select row_id,row_name,row_num; 
          set i := i+1;
    end while;
     close gs;
end;

call p5();  

-- 使用游标最主要的是能够针对每一次查出来的结果进行一些操做
drop procedure p6;
create procedure p6()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare count_r int;
     declare i int default 0;
    
     declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
     select count(*) into count_r from goods;
      
     open gs;
     while i<count_r do
          fetch gs into row_id,row_name,row_num;
          if row_num>25 then select concat(row_name,'比较多'); 
          elseif row_num=25 then select concat(row_name,'刚恰好');
          else select concat(row_name,'有点少');
          end if;        
          set i := i+1;
    end while;
     close gs;
end;

call p6();

-- 第三种方式:游标越界时候使用标志,利用标识来结束
-- 在mysql cursor中,能够使用declare continue handler来操做一个越界标识
-- declare continue handler for not found statement;
drop procedure p7;
create procedure p7()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     
     declare you int default 1;
     declare gs cursor for select id,name,num from goods;
     declare continue handler for not found set you:=0;
     
     open gs;
      while you!=0 do
           fetch gs into row_id,row_name,row_num;
           if you!=0 then select row_num,row_name;
           end if;      
     end while;
     close gs;
end;
call p7();
相关文章
相关标签/搜索