> 文章列表 > Oracle之PL/SQL程序包(九)

Oracle之PL/SQL程序包(九)

Oracle之PL/SQL程序包(九)

1、程序包概述

程序包是对相关过程、函数、变量游标和异常等对象的封装。程序包由规范(包头)和主体(包体)两部分组成。
规范中可以声明程序包中公共对象,包括类型、变量、常量、异常、游标规范和子程序规范等。
主体中可以声明程序包私有对象和实现在包规范中声明的子程序和游标

2、创建程序包的语法

程序包规范语法:

create or replace package package_name is|as
[public item declarations]
[subprogram specification]
end [package_name];

package_name:包的名称
public item declarations:公共声明部分
subprogram specification:声明PL/SQL子程序
程序包主体语法:

create or replace package body package_name is|as
[private item declarations]
[subprogram specification]
[begin
initialization]
end [package_name];

package_name 是包的名称
public type and item declarations:私有声明部分
subprogram specification:子程序体
如:

--定义包头
create or replace package pack1
isaa number:=9;procedure insert_dept(v_dept in dept%rowtype);function fun(v1 number,v2 number) return number;
end;
--包体实现
create or replace package body pack1
isbb number:=10;procedure insert_dept(v_dept in dept%rowtype) isbegininsert into dept values(v_dept.deptno,v_dept.dname,v_dept.loc);end;function fun(v1 number,v2 number) return number isbeginreturn v1+v2;end;
end;
--调用包
select pack1.fun(2,3) from dual;

3、程序包中的游标

油表的定义分为游标规范和游标主体两部分。在包规范中声明游标规范时必须使用 return 子句指定游标的返回类型。return 子句指定的数据类型可以是:
1、用 %rowtype 属性引用表定义的记录类型
2、程序员定义的记录类型,例如 type emprectyp is record(emp_id integer, salary real)来定义。
3、不可以是 number、varchar2、%type等类型
如:

--定义包头
create or replace package pack2
is--定义包头中的游标,存储员工表的数据cursor cur_emp return emp%rowtype;--定义存储过程,遍历游标中的数据procedure p_showemp;
end;
--创建包体
create or replace package body pack2
iscursor cur_emp return emp%rowtype is select * from emp;procedure p_showempisv_e emp%rowtype;beginopen cur_emp;loopfetch cur_emp into v_e;exit when cur_emp%notfound; dbms_output.put_line(v_e.empno||','||v_e.ename);end loop;close cur_emp;end;
end;
--调用包
beginpack2.p_showemp();
end;

REF游标

--创建包头
create or replace package pack3 istype refcur is ref cursor;procedure mycursor_use;
end;
--创建包体
create or replace package body pack3 isprocedure mycursor_use ismycursor refcur;v_emp emp%rowtype;beginopen mycursor for select * from emp;fetch mycursor into v_emp;while mycursor%found loopdbms_output.put_line(v_emp.ename);fetch mycursor into v_emp;end loop;close mycursor;end;
end;
--调用包
beginpack3.mycursor_use();
end;

4、内置程序包

可以扩展数据库的功能,为PL/SQL提供对SQL功能的访问,用户SYS拥有所有程序包,是公有同义词,可以由任何用户访问。一些内置程序包如下:

程序包名称 说明
standard和dbms_standard 定义和扩展PL/SQL语言环境
dbms_lob 提供对LOB数据类型进行操作的功能
dbms_output 处理PL/SQL块和子程序输出调试信息
dbms_random 提供随机数生成器
dbms_sql 允许用户使用动态SQL
dbms_xmldom 用DOM模型读写XML类型的数据
dbms_xmlparser XML解析,处理XML文档内容和结构
dbms_xmlquery 提供将数据转换为XML类型的功能
dbms_xslprocessor 提供XSLT功能,转换XML文档
utl_file 用PL/SQL程序来读写操作系统文本文件

1、dbms_random 包的使用

1、用来产生随机的数字、字符、日期
--产生随机整数
select dbms_random.random from dual;
--产生一个100以内的随机整数
select abs(mod(dbms_random.random,100)) from dual;
--value会返回一个大于等于0但是小于1的树
select dbms_random.value from dual;
--对于指定范围内的数,要加入low_value和high_value
--0到100之间的小数
select dbms_random.value(0,100) from dual;
--产生0到100之间的整数
select trunc(dbms_random.value(0,100)) from dual;
2、string 函数生成随机文本字符串,可以指定字符串的类型和所希望的长度。

‘U’ 用来生成大写字符,‘L’ 用来生成小写字符,‘A’ 用来生成大小写混合字符,‘P’ 表示字符串由任意可打印字符构成,‘X’ 表示字符串由大写字符和数字构成。

select dbms_random.string('A',5) from dual;
3、返回某年内的随机日期,分两步:
1、日期对应的内部整数,用格式 ‘J’
select to_char(to_date('01/01/23','mm/dd/yy'),'J') from dual;
2、内部整数对应的日期,用格式‘J’
select to_date(trunc(dbms_random.value(2459946,2459946+365)),'J') from dual;

2、dbms_job包的用法

--创建测试类
create table mytable1(a date);
--创建一个自定义过程
create or replace procedure test1
is
begininsert into mytable1 values(sysdate);
end;declarev number;
begin--创建jobdbms_job.submit(v,'test1;',sysdate,'sysdate+1/1440');--运行jobdbms_job.run(v);--删除job--dbms_job.remove(v);
end;select * from mytable1;

3、utl_file 包的用法

--创建一个Oracle的目录
create or replace directory 目录名称 as '目录路径';
create or replace directory FILEPATH as 'd:/SQL';
declare--声明文件类型的变量f utl_file.file_type;
begin--1、打开文件 utl_file.fopen(创建的directory,文件名,字符串),打开方式分三种w:写,r:读,a:追加f:=utl_file.fopen('FILEPATH','test.txt','a');--2、读或写文件 utl_file.put_line(文件类型变量,要写的文件内容)utl_file.put_line(f,'aa');utl_file.put_line(f,'bb');utl_file.put_line(f,'cc');utl_file.put_line(f,'dd');--3、关闭文件 utl_file.fclose(文件类型变量)utl_file.fclose(f);
end;declare--声明文件类型的变量f utl_file.file_type;--声明一个变量保存文件中的一条记录str varchar2(200);
begin--1、打开文件 f:=utl_file.fopen('FILEPATH','test.txt','r');--2、读或写文件utl_file.get_line(f,str);dbms_output.put_line(str);utl_file.get_line(f,str);dbms_output.put_line(str);utl_file.get_line(f,str);dbms_output.put_line(str);utl_file.get_line(f,str);dbms_output.put_line(str);--3、关闭文件utl_file.fclose(f);
end;declare--声明文件类型的变量f utl_file.file_type;
begin--打开文件f:=utl_file.fopen('FILEPATH','test.txt','r');--2、读或写文件loopdeclare--声明一个变量保存文件中的一行记录str varchar2(200);beginutl_file.get_line(f,str);dbms_output.put_line(str);exceptionwhen no_data_found thenexit;end;end loop;--3、关闭文件utl_file.fclose(f);
end; 

股市行情