Oracle Cursor 遊標總結(簡單樣例附帶表結構)

Oracle Cursor 遊標總結

一,隱式遊標
隱式遊標的屬性 返回值類型 意 義
SQL%ROWCOUNT 整型 代表DML語句成功執行的數據行數
SQL%FOUND 布爾型 值爲TRUE代表插入、刪除、更新或單行查詢操作成功
SQL%NOTFOUND 布爾型 與SQL%FOUND屬性返回值相反
SQL%ISOPEN 布爾型 DML執行過程中爲真,結束後爲假
二,顯示遊標
遊標的屬性 返回值類型 意義
%ROWCOUNT 整型 獲得FETCH語句返回的數據行數
%FOUND 布爾型 最近的FETCH語句返回一行數據則爲真,否則爲假
%NOTFOUND 布爾型 與%FOUND屬性返回值相反
%ISOPEN 布爾型 遊標已經打開時值爲真,否則爲假

三,測試表

create table t_raynor(
       id number(5),
       name varchar(20),
       age number(5)
       
);
insert into t_raynor values(1,'ray',22);
insert into t_raynor values(2,'sha',22);
insert into t_raynor values(3,'ray',null);
insert into t_raynor values(4,'sha',null);
insert into t_raynor values(5,'ray',null);

在這裏插入圖片描述
隱式遊標

declare
 cot number ;
begin
       update t_raynor t set t.age = '' where t.id in (3,4,5) and t.age is not null;
       cot := sql%rowcount;
       dbms_output.put_line('num:'||cot);     
end;

在這裏插入圖片描述
顯示遊標

–顯示遊標(提取ID=1 的數據)

 DECLARE
   cid number;
   CURSOR my_cursor is
     select id from t_raynor where id = 1;
 BEGIN
   open my_cursor;--打開
   fetch my_cursor INTO cid;--獲取數據
   dbms_output.put_line('num:' || cid);
   close my_cursor; --關閉
 END;

在這裏插入圖片描述
–顯示遊標(提取ID=1,2,3 的數據)

 DECLARE
   cid number;
   CURSOR my_cursor is
     select id from t_raynor where id in(1,2,3);
 BEGIN
   open my_cursor;--打開
   for i in 1..3 loop
     fetch my_cursor INTO cid;--獲取數據
     dbms_output.put_line('id:' || cid);
   end loop;
   close my_cursor; --關閉
 END;

在這裏插入圖片描述

–顯示遊標(提取ID=1,2,3 的數據)

DECLARE
   CURSOR my_cursor IS
     select id from t_raynor where id in(1,2,3);
 BEGIN
   FOR aa IN my_cursor LOOP
      dbms_output.put_line('id:' || aa.id);  
  END LOOP;
 END;

在這裏插入圖片描述

–顯示遊標(提取ID=1,2,3 的數據) :快速遊標

DECLARE
   
 BEGIN
   FOR aa IN (select id from t_raynor where id in(1,2,3)) LOOP
      dbms_output.put_line('id:' || aa.id);         
   END LOOP;
 END;

在這裏插入圖片描述

–顯示遊標的屬性 1

 DECLARE
       CURSOR my_cursor IS
         select id from t_raynor where id in(1,2,3);
     BEGIN
       open my_cursor;
       if my_cursor%isopen THEN
         dbms_output.put_line('OPEN' );
       end if;  
     END;

在這裏插入圖片描述
– 顯示遊標的屬性 2

 DECLARE
   v_id number;
   CURSOR my_cursor(p_id number) IS
     select id from t_raynor where id in(p_id,2,3);--定義遊標的參數可以作爲SELECT語句的條件
 BEGIN
   open my_cursor(1);--OPEN的時候需要 寫入參數值
   LOOP
     FETCH my_cursor INTO v_id;
     EXIT WHEN  my_cursor%NOTFOUND;--當遊標循環完成 退出
     DBMS_OUTPUT.put_line(v_id);
   END LOOP;  
 END;

在這裏插入圖片描述

– 顯示遊標的屬性 2

 DECLARE
   v_id number :=1;--通過變量給遊標傳值
   CURSOR my_cursor IS
     select id from t_raynor where id in(v_id,2,3);
 BEGIN
   open my_cursor;
   LOOP
     FETCH my_cursor INTO v_id;
     EXIT WHEN  my_cursor%NOTFOUND;
     DBMS_OUTPUT.put_line(v_id);
   END LOOP;  
 END;

在這裏插入圖片描述

–動態SEL

 DECLARE
   v_id number;
   str varchar2(200);  
 BEGIN
   str:='select id from t_raynor where id =1';
   execute immediate str into v_id;--SELECT...INTO...語句存放在STR字符串中,通過EXECUTE語句執行。
   dbms_output.put_line(v_id);  
 END; 

在這裏插入圖片描述

 DECLARE
   TYPE cur_type  is ref cursor;
   my_cur cur_type;
   my_rec t_raynor%rowtype;
   str varchar2(200);
   letter char:= 'a'; 
 BEGIN   
   loop
       str:='select name from t_raynor where name like ''%'||letter||'%'' ';
       open my_cur for str;
       dbms_output.put_line('包含字母'||letter||'的名字:');   
       
       loop
            fetch my_cur into my_rec.name;  
            exit when my_cur%notfound;  
            dbms_output.put_line(my_rec.name);  
       end loop;
       
       exit when letter='z';   
       letter:=chr(ascii(letter)+1);   
   end loop;  
 END; 

在這裏插入圖片描述

–複雜更新

 declare
           cot number ;
           scot number;
    begin
      scot := 0;
      for aa in (SELECT t.age, t.name FROM t_raynor t where t.age is not null) loop
         
        update t_raynor tr
           set tr.age = aa.age
         where tr.name = aa.name
           and tr.age is null;
           cot := sql%rowcount;
           dbms_output.put_line('cot:'||cot);
           scot := scot + cot;
            
      end loop;
          dbms_output.put_line('影響的總行數:'||scot);
    end;

在這裏插入圖片描述

declare
begin
  
update t_raynor t
   set t.age =
       (select t1.age
          from t_raynor t1
         where t1.name = t.name
         and t1.age is not null
           and rownum = 1)
 where exists (SELECT 1 from t_raynor t2 where t2.name = t.name);
  dbms_output.put_line('影響的總行數:'||sql%rowcount);

end;

在這裏插入圖片描述
在這裏插入圖片描述

-------------------- ref cursor

DECLARE
  rz1 sys_refcursor;
  ray t_raynor%rowtype; 
BEGIN
  open rz1 for
    SELECT * FROM t_raynor;     
  fetch rz1
    into ray;
  loop
    dbms_output.put_line(ray.id);
    fetch rz1
      into ray;
    exit when rz1%notfound;
  end loop;
END;

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章