一、概念
? REF游標和游標變數用于處理運行時動態執行的SQL查詢的結果集,
? 創建游標變數有兩個步驟:
? 宣告REF游標型別
? 宣告REF游標型別的游標變數
? 宣告REF游標的語法:
type 游標型別名 is ref cursor [return 回傳值型別]
二、區別
? 靜態游標和REF游標的區別:
? 靜態游標是靜態定義,REF游標是動態關聯,
? 使用REF游標需REF游標變數,
? REF游標能作為引數進行傳遞,而靜態游標是不能的,
三、優勢
? 游標變數與游標相比較:
? 游標只能處理靜態的查詢語言
? 游標變數可以處理動態查詢陳述句的結果集
四、實體
declare --強型別的游標型別 type strong_cursor_type is ref cursor return emp%rowtype; --弱型別的游標型別 type weak_cursor_type is ref cursor; --變數定義 strong_cursor strong_cursor_type; weak_cursor weak_cursor_type; v_emp emp%rowtype; begin open strong_cursor for select * from emp; loop fetch strong_cursor into v_emp; exit when strong_cursor%notfound; dbms_output.put_line(v_emp.empno || '-' || v_emp.ename); end loop; close strong_cursor; open weak_cursor for '&sql'; loop fetch weak_cursor into v_emp; exit when weak_cursor%notfound; dbms_output.put_line(v_emp.empno || '-' || v_emp.ename); end loop; close weak_cursor; end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/499131.html
標籤:Oracle
