oracle學習筆記(二十二) REF 動態游標


動態游標

定義語法

--聲明
$cursor_name$ sys_refcursor

--打開動態游標
open $cursor_name$ is 查詢語句;

--關閉游標
close $cursor_name$;

--聲明動態游標類型
type $type_name$ is ref cursor;

--聲明一個動態游標變量
$v_cursor_name$ type_my_ref;

使用

動態游標可以獲得不同的結果集,可以設置條件,返回不同的結果集,一般和過程一起使用

--創建過程list
create or replace procedure list(result_set in out sys_refcursor, which in number)
is   
begin
  --打開動態游標時在為它指定查詢語句
  --1就是返回員工表,其他就返回部門表
  if which=1 then
    open result_set for select * from employee;
  else
    open result_set for select * from department;
  end if;
end;
/

--調用list過程,根據輸入的數字輸出某個表的全部信息
declare
   --聲明一個動態游標類型
   type type_my_ref is ref cursor;
   --聲明一個動態游標變量
   my_ref type_my_ref;
   emp employee%rowtype;
   dept department%rowtype;
   
   which number default &請輸入;
begin
  --得到一個查詢結果集
  list(my_ref, which);
 loop
   if which=1 then /* handle employee */
      fetch my_ref into emp;
      exit when my_ref%notfound;
      dbms_output.put_line(emp.empno||','||emp.ename||','||emp.job||','||emp.sal);
      
   else            /* handle department */
      fetch my_ref into dept;
      exit when my_ref%notfound;
      dbms_output.put_line(dept.deptno||','||dept.dname||','||dept.loc);
   end if;
 end loop;
 --close cursor
 close my_ref;
end;
/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM