天天看點

oracle cursor遊标總結

遊标(Cursor):用來查詢資料庫,擷取記錄集合(結果集)的指針,可以讓開發者一次通路一行結果集,在每條結果集上作操作。

 遊标可分為:

1.靜态遊标:分為顯式(explicit)遊标和隐式(implicit)遊标。

2.REF遊标:是一種引用類型,類似于指針。

1、靜态遊标

1.1顯式遊标

定義格式:   

CURSOR 遊标名 ( 參數 )  IS

Select 語句 FOR UPDATE [OF [schema.]table.column[,[schema.]table.column]..

[nowait]

例子1 :無參數,打開關閉遊标

set serveroutput on size 10000000 ;

 create or replace procedure TEST is
  cursor c1 is
    select tname from tab;
  pname varchar2(32);
begin
  open c1;
  loop
    fetch c1
      into pname;
    exit when c1%notfound;
    dbms_output.put_line(pname);
  end loop;
  close c1;
end TEST
exec test;
           

例子2 :參數使用,參數使用方法和存儲過程一樣

create or replace procedure TEST is
  cursor c1(pname in varchar2) is
    select tname from tab where tname like pname;
  pname varchar2(32);
begin
  open c1(‘T%’);
  loop
    fetch c1
      into pname;
    exit when c1%notfound;
    dbms_output.put_line(pname);
  end loop;
  close c1;
end TEST;
           

1.2隐式遊标

不用明确建立遊标變量,分兩種:

1.在PL/SQL中使用DML語言,使用ORACLE提供的名為“SQL”的隐示遊标。

舉例:

declare
begin
  update departments set department_name = department_name;
  –where 1=2;
  dbms_output.put_line(‘update ‘ || sql%rowcount || ‘ records’);
end;
/
           

2.CURSOR FOR LOOP,用于for loop 語句

舉例:

例子1:無參數,使用循環,無須打開關閉,本人這種方式

create or replace procedure TEST is
  cursor c1 is
    select tname from tab;
begin
  for rr in c1 loop
    dbms_output.put_line(rr.tname);
  end loop;
end TEST;
           

例子1:有參數,使用循環,無須打開關閉,本人這種方式

create or replace procedure TEST is
  cursor c1(pname in varchar2) is
    select tname from tab where tname like pname;
begin
  for rr in c1(‘T%’) loop
    dbms_output.put_line(rr.tname);
  end loop;
end TEST;
           

1.3遊标常用屬性:

%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;
    End if;
  End loop;

  If emps%isopen then
    Close emps; /*  關閉遊标  */
  End if
End;
/
           

 1.4 遊标的更新和删除

在PL/SQL中依然可以使用UPDATE和DELETE語句更新或删除資料行。顯式遊标隻有在需要獲得多行資料的情況下使用。PL/SQL提供了僅僅使用遊标就可以執行删除或更新記錄的方法。

UPDATE或DELETE語句中的WHERE CURRENT OF子串專門處理要執行UPDATE或DELETE操作的表中取出的最近的資料。要使用這個方法,在聲明遊标時必須使用FOR UPDATE子串,當對話使用FOR UPDATE子串打開一個遊标時,所有傳回集中的資料行都将處于行級(ROW-LEVEL)獨占式鎖定,其他對象隻能查詢這些資料行,不能進行UPDATE、DELETE或SELECT…FOR UPDATE操作。

在多表查詢中,使用OF子句來鎖定特定的表,如果忽略了OF子句,那麼所有表中選擇的資料行都将被鎖定。如果這些資料行已經被其他會話鎖定,那麼正常情況下ORACLE将等待,直到資料行解鎖。

在UPDATE和DELETE中使用WHERE CURRENT OF子串的文法如下:

WHERE{CURRENT OF cursor_name|search_condition}

例:

create or replace procedure pc_SetVersionValid(PFlowsID in integer) is
  Cursor c1 is
    select *
      from wf_flows
     where flowname in
           (select flowname from wf_flows where flowsid = PFlowsID)
       for update;
  r c1%rowtype;
  v integer;
begin
  open c1;
  fetch c1
    into r;
  while c1%found loop
    if r.flowsid = PFlowsID then
      v := 1;
    else
      v := 0;
    end if;  

    UPDATE wf_flows SET isValid = v WHERE CURRENT OF c1; 
    fetch c1
      into r;
  end loop;
  close c1;
  commit;
end;
           

 顯式和隐式遊标的差別:

盡量使用隐式遊标,避免編寫附加的遊标控制代碼(聲明,打開,擷取,關閉),也不需要聲明變量來儲存從遊标中擷取的資料。

2、REF CURSOR遊标

動态遊标,在運作的時候才能确定遊标使用的查詢。可以分為:

create or replace procedure TEST is
  sqlstr varchar2(500);
  type RefCur is ref cursor;
  c1 refcur;
begin
  sqlstr := ’select * from tab’;
  open c1 for sqlstr;
  close c1;
end;
           

用REF CURSOR實作BULK功能

1. 可以加速INSERT, UPDATE, DELETE語句的執行,也就是用FORALL語句來替代循環語句。

2. 加速SELECT,用BULK COLLECT INTO 來替代INTO。

SQL> create table tab2  as select empno ID, ename NAME, sal SALARY from emp where 1=2;
create or replace procedure REF_BULK is
/*  定義複雜類型 */
type empcurtyp  is ref cursor; 
type idlist  is table of emp.empno%type;
type namelist  is table of emp.ename%type;
type sallist  is table of emp.sal%type;
  /* 定義變量  */
emp_cv  empcurtyp;
ids  idlist;
names namelist;
sals sallist;
row_cnt number;
begin
open emp_cv for select empno, ename, sal from emp;
fetch emp_cv  BULK COLLECT  INTO ids, names, sals; 
---将字段成批放入變量中,此時變量是一個集合
close emp_cv;

for i in ids.first .. ids.last loop
dbms_output.put_line(’ || ids(i) || ‘ || names(i) ||’ salary=’ || sals(i));
end loop;
 
FORALL  i  IN  ids.first .. ids.last 
insert into tab2 values (ids(i), names(i), sals(i));
commit;
select count(*) into row_cnt from tab2;
dbms_output.put_line(’———————————–’);
dbms_output.put_line(’The row number of tab2 is ‘ || row_cnt);
end REF_BULK;
           

3、cursor 和 ref cursor的差別

從技術底層看,兩者是相同的。普通plsql cursor在定義時是“靜态”的。而

Ref cursors可以動态打開。

例如下面例子:

Declare
type rc is ref cursor;
cursor c is select * from dual;

l_cursor rc;
begin
if ( to_char(sysdate,’dd’) = 30 ) then
       open l_cursor for ’select * from emp’;
elsif ( to_char(sysdate,’dd’) = 29 ) then
       open l_cursor for select * from dept;
else
       open l_cursor for select * from dual;
end if;
open c;
end;
/ 
           

rc根據邏輯動态打開;而遊标c定義好了隻有就無法修改了。

ref cursor可以傳回給用戶端,cursor則不行。

cursor可以是全局的global ,ref cursor則必須定義在過程或函數中。

ref cursor可以在子程式間傳遞,cursor則不行。

cursor中定義的靜态sql比ref cursor效率高,是以ref cursor通常用在:向用戶端傳回結果集。

繼續閱讀