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();
相關文章
相關標籤/搜索