【PL/SQL 學習】隱式遊標學習

楊奇龍發表於2011-02-25
--oracle 會為每一個非顯示遊標的sql dml 語句都建立一個隱式遊標,隱式遊標也稱為sql 遊標。與顯示遊標不同,不能對一個隱式遊標執行open,close和fetch語句。oracle 隱式的開啟sql遊標,處理sql遊標,然後再關閉該遊標。
declare
  vid t.object_id%type;
  vowner t.owner%type;
 begin
   select object_id ,owner into vid,vowner from t where rownum <2;
   if sql%isopen then
     dbms_output.put_line('it is not possiable');
     else
       dbms_output.put_line('vid is  '||vid||'  vowner is '||vowner);
       dbms_output.put_line('%isopen 的屬性為false');
   end if;
 end;
輸出為:
vid is  1  vowner is SYS
%isopen 的屬性為false
--和顯示遊標一樣,隱式遊標也有四大屬性%FOUND,%ISOPEN,%NOTFOUND,%ROWCOUNT.由於隱式遊標沒有名次,oracle提供了一種方法在屬性前面加 關鍵字 SQL,這樣我們就可以使用這些屬性了。
例子如下:
declare
  vid t.object_id%type;
  vowner t.owner%type;
  rowsnum integer;
 begin
   select object_id ,owner into vid,vowner from t where rownum <2;
   rowsnum := sql%rowcount;
     dbms_output.put_line('vid is  '||vid||'  vowner is '||vowner);
     dbms_output.put_line('rowsnum is '||rowsnum);
     if sql%found then
        dbms_output.put_line('sql%found is true;');
     else
        dbms_output.put_line('sql%found is false;');
     end if;
     if sql%notfound then
         dbms_output.put_line('sql%notfound is true;');
     else
         dbms_output.put_line('sql%notfound is false;');
     end if;
end;  
輸出為:
vid is  1  vowner is SYS
rowsnum is 1
sql%found is true;
sql%notfound is false;
 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-687917/,如需轉載,請註明出處,否則將追究法律責任。

相關文章