一、包的作用
? Oracle中包的概念與Java中包的概念非常類似,只是Java中的包是為了分類管理類,但是關鍵字都是package,
? 在一個大型專案中,可能有很多模塊,而每個模塊又有自己的程序、函式等,而這些程序、函式默認是放在一起的(如在PL/SQL中,程序默認都是放在一起的,即Procedures中),這些非常不方便查詢和維護,甚至會發生誤洗掉的事件,所以通過使用包就可以分類管理程序和函式,
? 包中還可以自定義自定義型別,從而在程序和函式中可以直接使用自定義變數,
二、包的構成
? 包規范部分
? 包體部分
--包規范定義語法 create or replace package 包名 as |is --定義存盤程序 --定義函式 --定義ref游標型別 end 包名; --包體定義語法 create or replace package body 包名 is |as --實作存盤程序 --實作函式 end 包名;
三、包的實體
? 定義包:
--定義包規范 create or replace package getemp_package as --定義一個游標型別 type emp_cursor_type is ref cursor; --定義一個存盤程序 procedure getemp(p_sal in number,c_emp out emp_cursor_type); end getemp_package; --定義包體 create or replace package body getemp_package as --實作存盤程序 procedure getemp(p_sal in number,c_emp out emp_cursor_type) as begin --打開游標 open c_emp for select * from emp where sal>p_sal; end getemp; end getemp_package;
? 呼叫包:
set serveroutput on; declare c_out getemp_package.emp_cursor_type; v_emp emp%rowtype; begin getemp_package.getemp(p_sal=>900,c_emp=>c_out); loop fetch c_out into v_emp; exit when c_out%notfound; dbms_output.put_line(v_emp.empno || '-' || v_emp.ename); end loop; end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/499135.html
標籤:Oracle
