Oracle學習筆記 6 程式包使用

2021-08-30 18:13:19 字數 2552 閱讀 1895

程式包主體

首先執行:

create or replace package mypack

isprocedure employee2_proc(employeeid number);

function employee2_function return varchar2 ;

end;

/其次執行:

create or replace package body mypack as

procedure employee2_proc(employeeid number)

isfirstname 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;

程式包 學習筆記

示例一 根據輸入的部門編號找到這個部門的所有員工 首先建立包,定義包規範 create orreplace package getemp pkg is function getemp fun p dno dept.deptno type return sys refcursor 返回弱型別游標 en...

PL SQL學習筆記 程式包

一 程式包的說明 create or replace package p xland ispragma serially reusable v val varchar2 32 function set val ref val varchar2 return varchar2 end p xland ...

Oracle中使用程式包學習例項

1 建立程式包規範,可以宣告變數,型別,子程式等 create or replace package pack me is procedure emp proc num number function emp func empno number return varchar2 end pack me...