Oracle学习笔记 6 程序包使用

程序包主体
  程序包是对相关过程、函数、变量、游标和异常等对象的封装,以下代码演示了程序包的使用:
  首先执行:
  create or replace package mypack
is
   procedure employee2_proc(employeeid number);
   function employee2_function return varchar2 ;
end;
/
其次执行:
create or replace package body mypack as
procedure employee2_proc(employeeid  number)
is
  firstname varchar2(20);
  lastname varchar2(20);
  divisionid varchar2(20);
begin
   select first_name,last_name,division_id into firstname,lastname,divisionid
      from employees2  where employee_id = employeeid ;
   dbms_output.put_line('first_name = '||firstname || 'lastname='||lastname || 'divisionid ='||   divisionid);
end employee2_proc;
function employee2_function
return varchar2 is
    jobid varchar2(20);
    sal number(8);
begin
   select job_id,salary into jobid,sal from employees2 where employee_id =2;
   if jobid = 'MGR' then
      dbms_output.put_line('job = Manager' || ' salary = '||sal);
   end if;
end employee2_function;
end mypack;
/

以下代码执行程序包中的employee2_proc过程
     Set serveroutput on
     execute mypack.employee2_proc(2);
以下代码执行程序包中的employee2_function函数



declare
  empinfo varchar2(50);
begin
    empinfo := mypack.employee2_function;
    dbms_output.put_line('empinfo ='|| empinfo);
end;
/

程序包中的游标:
1. 创建程序包:
      create or replace package my_cur as
cursor employeecur return employees2;
end;
    /
2. 创建程序包主体:
     create or replace package body my_cur as
       cursor employeecur return employees2 is
      select * from employees2 where salary >120000;
     end;
      /
      
执行以上程序包中的my_cur 游标。
declare
  myrecord employees2%rowtype;
begin
   open my_cur.employeecur;
loop
    fetch my_cur.employeecur into myrecord;
    exit when my_cur.employeecur %notfound;
    dbms_output.put_line('employee_id ='||myrecord.employee_id || ' 
          firstname ='|| myrecord.first_name ||'  lastname ='|| myrecord.last_name);
end loop;
end;
/
     
获取子程序和程序包的信息:
COLUMN OBJECT_NAME FORMAT A18
SELECT object_name, object_type
FROM USER_OBJECTS
WHERE object_type IN ('PROCEDURE', 'FUNCTION',
'PACKAGE', 'PACKAGE BODY');

获取user_source 视图结构:
      DESC USER_SOURCE
获取程序包中的子程序 TEST 的源代码。
    COLUMN LINE FORMAT 9999
COLUMN TEXT FORMAT A50

SELECT line, text FROM USER_SOURCE
WHERE NAME='TEST';
获取程序包中的子程序规范信息:
    Desc pack_me;

你可能感兴趣的:(oracle)