原文地址:https://www.cnblogs.com/hoaprox/p/5316444.html
1,简单的包。
创建包规范:
create or replace package pack_test1 is -- 定义过程1 procedure p_test1(p_1 in varchar2); -- 定义函数1 function f_test1(p_1 in varchar2) return varchar2; end pack_test1;
创建包体:
create or replace package body pack_test1 is -- 包全局变量1 v_param1 varchar(20) := 'default'; -- 实现过程1 procedure p_test1(p_1 in varchar2) is begin dbms_output.put_line('p_1的值为:'|| p_1); dbms_output.put_line('全局变量的值为:'||v_param1); -- 改变全局变量 v_param1 := p_1; dbms_output.put_line('改变后的全局变量值为:'||v_param1); end; -- 实现函数1 function f_test1(p_1 in varchar2) return varchar2 is v_rt varchar2(50); begin dbms_output.put_line('获取的全局变量值为:'||v_param1); v_rt := v_param1||'-'||p_1; dbms_output.put_line('返回值为:'||v_rt); return v_rt; end f_test1; end pack_test1; ---------------------
调用包:
在一般的sql窗口可以使用以下方法:
-- 调用过程 call pack_test1.p_test1('参数1'); -- 调用函数 select pack_test1.f_test1('参数2') from dual;
在命令窗口可以使用:
exec pack_test1.p_test1('参数1');--执行此包 存在版本问题 看不到输出
2,包间调用
刚学pl/sql编程,写了两个package。pkg_temp_fn和pkg_temp_fn2。内容涉及pl/sql基本语法,游标,存储过程(in,out),函数(有返回值)。
- pkg_temp_fn内有一个function f_getdept(v_deptid number) return VARCHAR2和一个procedure p_getinfo(v_mapid NUMBER)
创建包规范
1 create or replace package pkg_temp_fn is 2 function f_getdept(v_deptid number) return VARCHAR2; 3 --reference pkg_temp_fn2 4 procedure p_getinfo(v_mapid NUMBER/*,v_pname VARCHAR2(20),v_deptno VARCHAR2(15)*/); 5 -- procedure p_main_process; 6 end pkg_temp_fn; 7 --3/24/2016 8 --var map_id number; 9 --exec pkg_temp_fn.p_getinfo(&map_id);调用方法
创建包体
1 create or replace package body pkg_temp_fn 2 is 3 function f_getdept(v_deptid number) return VARCHAR2 4 is 5 deptno varchar2(15); 6 begin 7 select y.dept_no into deptno from t_fn_dept y where y.dept_id=v_deptid; 8 return deptno; 9 end f_getdept; 10 11 procedure p_getinfo(v_mapid NUMBER) 12 is 13 cursor xing is select x.person_name,x.dept from t_fn_person x where x.map_id=v_mapid; 14 pname VARCHAR2(20); 15 pdept number; 16 v_deptno VARCHAR2(15); 17 begin 18 open xing; 19 loop 20 fetch xing into pname,pdept; 21 exit when xing%notfound; 22 v_deptno:=f_getdept(pdept);--call function 23 dbms_output.put_line(pname||'''s dept is :'||v_deptno); 24 end loop; 25 close xing; 26 end p_getinfo; 27 end pkg_temp_fn;
- kg_temp_fn2内有一个function f_getdept(v_deptid number) return VARCHAR2、一个procedure p_getinfo(v_mapid NUMBER,v_pname out VARCHAR2,v_deptno out VARCHAR2),一个procedure p_main_process(v_mapid NUMBER)。
创建包规范
1 create or replace package pkg_temp_fn2 is 2 function f_getdept(v_deptid number) return VARCHAR2; 3 procedure p_getinfo(v_mapid NUMBER,v_pname out VARCHAR2,v_deptno out VARCHAR2); 4 procedure p_main_process(v_mapid NUMBER); 5 end pkg_temp_fn2; 6 --3/24/2016 7 --var map_id number; 8 --exec pkg_temp_fn2.p_main_process(&map_id);调用方法
创建包体
1 create or replace package body pkg_temp_fn2 2 is 3 function f_getdept(v_deptid number) return VARCHAR2 4 is 5 deptno varchar2(15); 6 begin 7 select y.dept_no into deptno from t_fn_dept y where y.dept_id=v_deptid; 8 return deptno; 9 end f_getdept; 10 11 procedure p_getinfo(v_mapid in NUMBER,v_pname out VARCHAR2,v_deptno out VARCHAR2) 12 is 13 cursor xing is select x.person_name,x.dept from t_fn_person x where x.map_id=v_mapid; 14 pdept number; 15 begin 16 open xing; 17 loop 18 fetch xing into v_pname,pdept; 19 exit when xing%notfound; 20 v_deptno:=f_getdept(pdept);-- 21 --dbms_output.put_line(pname||'''s dept is :'||v_deptno); 22 end loop; 23 close xing; 24 end p_getinfo; 25 procedure p_main_process(v_mapid NUMBER) 26 is 27 v_pname VARCHAR2(20); 28 v_deptno varchar2(15); 29 begin 30 p_getinfo(v_mapid,v_pname,v_deptno); 31 dbms_output.put_line(v_pname||'''s dept is :'||v_deptno); 32 end p_main_process; 33 end pkg_temp_fn2;
ps:
定义变量或者参数的的时候,类型不要有括号,否则报错。
标签:test1,存储,end,temp,PACKAGE,--,VARCHAR2,ORACLE,deptno From: https://www.cnblogs.com/eyesfree/p/17282480.html