superwei

          導航

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          oracle cursor 游標(轉載)

          ?

          游標

          用來查詢數據庫,獲取記錄集合(結果集)的指針,可以讓開發者一次訪問一行結果集,在每條結果集上作操作。


          分類

          靜態游標:
          分為顯式游標和隱式游標。

          REF游標:
          是一種引用類型,類似于指針。

          ?

          顯式游標

          ?CURSOR?游標名?(?參數?)?[返回值類型]?IS
          ??Select?語句


          生命周期:

          1.打開游標(OPEN)
          解析,綁定。。。不會從數據庫檢索數據

          2.從游標中獲取記錄(FETCH?INTO)
          執行查詢,返回結果集。通常定義局域變量作為從游標獲取數據的緩沖區。

          3.關閉游標(CLOSE)
          完成游標處理,用戶不能從游標中獲取行。還可以重新打開。


          選項:參數和返回類型


          set?serveroutput?on
          declare
          ?cursor?emp_cur?(?p_deptid?in?number)?is?
          select?*?from?employees?where?department_id?=?p_deptid;

          l_emp?employees%rowtype;
          begin
          ?dbms_output.put_line('Getting?employees?from?department?30');
          open?emp_cur(30);
          ?loop
          ??fetch?emp_cur?into?l_emp;
          ??exit?when?emp_cur%notfound;
          ??dbms_output.put_line('Employee?id?'||?l_emp.employee_id?||?'?is?');
          ??dbms_output.put_line(l_emp.first_name?||?'?'?||?l_emp.last_name);
          ?end?loop;
          ?close?emp_cur;

          ?dbms_output.put_line('Getting?employees?from?department?90');
          open?emp_cur(90);
          ?loop
          ??fetch?emp_cur?into?l_emp;
          ??exit?when?emp_cur%notfound;
          ??dbms_output.put_line('Employee?id?'||?l_emp.employee_id?||?'?is?');
          ??dbms_output.put_line(l_emp.first_name?||?'?'?||?l_emp.last_name);
          ?end?loop;
          ?close?emp_cur;
          end;
          /

          ?

          隱式游標

          不用明確建立游標變量,分兩種:
          1.在PL/SQL中使用DML語言,使用ORACLE提供的名為SQL的隱示游標
          2.CURSOR?FOR?LOOP,用于for?loop?語句


          1舉例:

          declare
          begin
          ?update?departments?set?department_name=department_name;
          ?--where?1=2;
          ?
          ?dbms_output.put_line('update?'||?sql%rowcount?||'?records');
          end;
          /


          2舉例:

          declare
          begin
          ?for?my_dept_rec?in?(?select?department_name,?department_id?from?departments)
          ?loop
          ??dbms_output.put_line(my_dept_rec.department_id?||?'?:?'?||?my_dept_rec.department_name);
          ?end?loop;
          end;
          /


          3舉例:

          單獨select?

          declare
          ?l_empno?emp.EMPLOYEE_ID%type;
          --?l_ename?emp.ename%type;
          begin
          ?select?EMPLOYEE_ID????
          ??into?l_empno
          ?from?emp;
          ?--where?rownum?=1;
          ?dbms_output.put_line(l_empno);
          end;
          /
          使用INTO獲取值,只能返回一行。

          ?

          游標屬性

          %FOUND:變量最后從游標中獲取記錄的時候,在結果集中找到了記錄。
          %NOTFOUND:變量最后從游標中獲取記錄的時候,在結果集中沒有找到記錄。
          %ROWCOUNT:當前時刻已經從游標中獲取的記錄數量。
          %ISOPEN:是否打開。


          Declare
          ?Cursor?emps?is
          ?Select?*?from?employees?where?rownum<6?order?by?1;
          ?
          ?Emp?employees%rowtype;
          ?Row?number?:=1;
          Begin
          ?Open?emps;
          ?Fetch?emps?into?emp;
          ?
          ?Loop
          ??If?emps%found?then
          ???Dbms_output.put_line('Looping?over?record?'||row||?'?of?'?||?emps%rowcount);
          ???Fetch?emps?into?emp;
          ???Row?:=?row?+?1;
          ??Elsif?emps%notfound?then
          ???Exit;??---exit?loop,?not?IF
          ??End?if;
          ?End?loop;
          ?
          ?If?emps%isopen?then
          ??Close?emps;
          ?End?if;
          End;
          /

          ?

          顯式和隱式游標的區別

          盡量使用隱式游標,避免編寫附加的游標控制代碼(聲明,打開,獲取,關閉),也不需要聲明變量來保存從游標中獲取的數據。

          ?

          REF?CURSOR游標

          動態游標,在運行的時候才能確定游標使用的查詢。分類:
          強類型(限制)REF?CURSOR,規定返回類型?
          弱類型(非限制)REF?CURSOR,不規定返回類型,可以獲取任何結果集。


          TYPE?ref_cursor_name?IS?REF?CURSOR?[RETURN?return_type]


          Declare
          ?Type?refcur_t?is?ref?cursor;
          ?
          ?Type?emp_refcur_t?is?ref?cursor?return?employee%rowtype;
          Begin
          ?Null;
          End;
          /


          強類型舉例:

          declare
          ?--聲明記錄類型
          ?type?emp_job_rec?is?record(
          ??employee_id?number,
          ??employee_name?varchar2(50),
          ??job_title?varchar2(30)
          ?);
          ?--聲明REF?CURSOR,返回值為該記錄類型
          ?type?emp_job_refcur_type?is?ref?cursor
          ??return?emp_job_rec;
          ?--定義REF?CURSOR游標的變量
          ?emp_refcur?emp_job_refcur_type;

          ?emp_job?emp_job_rec;
          begin
          ?open?emp_refcur?for
          ??select?e.employee_id,
          ????e.first_name?||?'?'?||e.last_name?"employee_name",
          ????j.job_title
          ??from?employees?e,?jobs?j
          ??where?e.job_id?=?j.job_id?and?rownum?<?11?order?by?1;

          ?fetch?emp_refcur?into?emp_job;
          ?while?emp_refcur%found?loop
          ??dbms_output.put_line(emp_job.employee_name?||?'''s?job?is?');
          ??dbms_output.put_line(emp_job.job_title);
          ??fetch?emp_refcur?into?emp_job;
          ?end?loop;
          end;

          posted on 2007-02-08 17:19 小辭猬 閱讀(346) 評論(0)  編輯  收藏 所屬分類: DataBase

          主站蜘蛛池模板: 尼勒克县| 高陵县| 丰镇市| 盈江县| 桂东县| 德州市| 聊城市| 莱西市| 驻马店市| 阿荣旗| 伽师县| 十堰市| 怀安县| 宣威市| 永康市| 昌吉市| 邵阳市| 龙胜| 安丘市| 区。| 津南区| 田林县| 丁青县| 化德县| 磐安县| 湖口县| 惠州市| 平顶山市| 瓦房店市| 兴国县| 乐安县| 浦江县| 张家口市| 阿荣旗| 青铜峡市| 大邑县| 南乐县| 会昌县| 张北县| 潮州市| 永安市|