oracle基礎之遊標的理解與使用

來源:互聯網
上載者:User

標籤:方向   manage   sele   ber   動態資料指標   資料   步驟   col   fetch   

關於遊標,首先要知道遊標的定義。

  遊標,是記憶體中的一款地區,用來存放select的結果集

  遊標用來處理從資料庫中檢索的多行記錄(使用select語句)。利用遊標,程式可以逐個的處理和遍曆一次索引返回的整個記錄集。

在資料庫中,存在兩種遊標:

一、顯示遊標(需要明確定義)

    顯示遊標被用於處理返回多行資料的select語句,遊標名通過CURSOR...IS語句顯示的賦給select語句。

    在PL/SQL中處理顯示遊標的四個步驟:

     1)聲明遊標:CURSOR   IS select語句

     2)為查詢開啟遊標: OPEN 遊標名稱

     3) 取得結果放入PL/SQL變數中:

         FETCH 遊標名稱 INTO list_of_variables;

                  FETCH  遊標名稱     INTO PL/SQL_record;

     4)關閉遊標:CLOSE 遊標名稱

     注意:在聲明遊標時, select語句不能包含INTO子句。當使用顯示遊標時,INTO子句是FETCH語句的一部分。

二、隱式遊標 
       所有的隱式遊標都被假設為只返回一條記錄。
       使用隱式遊標時,使用者無需進行聲明、開啟及關閉。PL/SQL隱含地開啟、處理,然後關掉遊標。
       例如:
       …….
       SELECT studentNo,studentName

       INTO curStudentNo,curStudentName

       FROM StudentRecord

       WHERE name=’gg’;

       上述遊標自動開啟,並把相關值賦給對應變數,然後關閉。執行完後,PL/SQL變數curStudentNo,curStudentName中已經有了值。
   2、 隱式遊標
     單條sql語句所產生的結果集合 
       用關鍵字SQL表示隱式遊標
        4個屬性 %rowcount  影響的記錄的行數  整數
                %found     影響到了記錄 true
                %notfound  沒有影響到記錄 true
                %isopen    是否開啟  布爾值 永遠是false
         多條sql語句 隱式遊標SQL永遠指的是最後一條sql語句的結果
         主要使用在update 和 delete語句上      


實際操作和例子:

   (1)FOR迴圈遊標 (常用的一種遊標)

 --<1>定義遊標
 --<2>定義遊標變數
 --<3>使用for迴圈來使用這個遊標

  --前向遊標 只能往一個方向走
  --效率很高
      declare
        --類型定義
        cursor cc is select empno,ename,job,sal
         from emp where job = ‘MANAGER‘;
        --定義一個遊標變數
        ccrec cc%rowtype;
      begin
         --for迴圈
         for ccrec in cc loop
            dbms_output.put_line(ccrec.empno||‘-‘||ccrec.ename||‘-‘||ccrec.job||‘-‘||ccrec.sal);
         end loop;       
      end;
   (2) fetch遊標
     --使用的時候 必須要明確的開啟和關閉
      declare
        --類型定義
        cursor cc is select empno,ename,job,sal
         from emp where job = ‘MANAGER‘;
        --定義一個遊標變數
        ccrec cc%rowtype;
      begin
        --開啟遊標
         open cc;
        --loop迴圈
         loop
            --提取一行資料到ccrec中 
            fetch cc into ccrec;         
            --判斷是否提取到值,沒取到值就退出
            --取到值cc%notfound 是false
            --取不到值cc%notfound 是true 
            exit when cc%notfound;
            dbms_output.put_line(ccrec.empno||‘-‘||ccrec.ename||‘-‘||ccrec.job||‘-‘||ccrec.sal);            
         end loop; 
        --關閉遊標
         close cc;  
      end;
  遊標的屬性4種
       %notfound  fetch是否提到資料 沒有true 提到false
       %found      fetch是否提到資料 有true 沒提到false
       %rowcount  已經取出的記錄的條數
       %isopen    布爾值 遊標是否開啟
   (3)參數遊標
 按部門編號的順序輸出部門經理的名字
     declare
       --部門
       cursor c1 is select deptno from dept;
       --參數遊標c2,定義參數的時候
       --只能指定類型,不能指定長度  
       --參數只能出現在select語句=號的右側
       cursor c2(no number,pjob varchar2) is select emp.* from emp
         where deptno = no and job=pjob;
       c1rec c1%rowtype;
       c2rec c2%rowtype;
       --定義變數的時候要指定長度
       v_job varchar2(20);
     begin
       --部門
        for c1rec in c1 loop
          --參數在遊標中使用
          for c2rec in c2(c1rec.deptno,‘MANAGER‘) loop
            dbms_output.put_line(c1rec.deptno||‘-‘||c2rec.ename);
          end loop; 
        end loop; 
     end; 
  (4)引用遊標/動態資料指標
       -- select語句是動態
     declare
       --定義一個類型(ref cursor)弱類型    
       type cur is ref cursor;
         --強型別(返回的結果集有要求)
       type cur1 is ref cursor return emp%rowtype;
       --定義一個ref cursor類型的變數   
       cura cur;
       c1rec emp%rowtype;
       c2rec dept%rowtype;
     begin
  DBMS_output.put_line(‘輸出員工‘)   ;       
       open cura for select * from emp;
       loop
         fetch cura into c1rec;   
         exit when cura%notfound;
         DBMS_output.put_line(c1rec.ename)   ;
       end loop ;
  DBMS_output.put_line(‘輸出部門‘)   ;
       open cura for select * from dept;
       loop
         fetch cura into c2rec;   
         exit when cura%notfound;
         DBMS_output.put_line(c2rec.dname)   ;
       end loop;  
       close cura;
    end;

oracle基礎之遊標的理解與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.