包由两个分离的部分组成:包头(PACKAGE)和包体(PACKAGEBODY)。包头是包的说明部分,是对外的操作接口,对应用是可见的;包体是包的代码和实现部分,对应用来说是不可见的黑盒。
       出现在包头中的称为公有元素,出现在包体中的称为私有元素,出现在包体的过程(或函数)中的称为局部变量。

创建包头的简要语句如下:

CREATE [OR REPLACE] PACKAGE 包名
{IS|AS}
公有变量定义
公有类型定义
公有游标定义
公有异常定义
函数说明
过程说明
END;

创建包体的简要语法如下:

CREATE [OR REPLACE] PACKAGE BODY 包名
{IS|AS}
私有变量定义
私有类型定义
私有游标定义
私有异常定义
函数定义
过程定义
END;

其它操作:

删除包头:
DROP PACKAGE 包头名
删除包体:
DROP PACKAGE BODY 包体名
重新编译包头:
ALTER PACKAGE 包名 COMPILE PACKAGE
重新编译包体:
ALTER PACKAGE 包名 COMPILE PACKAGE BODY

案例:对学生表infos提供一个增删改查的包,infos表内容如下图所示:

包中的内容结构如下:

程序结构 类型 参数 说明
v_infos_count 公有变量   学生总总数量,number类型
p_init 公有过程

p_max number

p_min number

最大值,最小值
p_list_infos 公有过程   显示学生列表数据
p_add_infos 公有过程

p_stuid infos.stuid%type,
p_stuname infos.stuname%type,
p_gender infos.gender%type,
p_age infos.age%type,
p_seat infos.seat%type,
p_enrolldate infos.enrolldate%type,
p_stuaddress infos.stuaddress%type,
p_classno infos.classno%type

增加一条学生记录
p_delete_infos 公有过程 p_stuid infos.stuid%type 根据stuid删除一条学生记录
p_edit_infos_name 公有过程

p_stuid infos.stuid%type
p_stuname infos.stuname%type

根据stuid修改学生的姓名
v_msg 私有变量   show message
v_max_age 私有变量   max age ,number
v_min_age 私有变量   min age ,number
f_exist_infos 私有函数

p_stuid infos.stuid%type

判断学生是否存在,

return boolean

p_show_msg 私有过程   show msg

包SQL:

(1)创建包头
create or replace package pck_infos
as
--总数量
v_infos_count number;
--初始化操作
procedure p_init(p_max number, p_min number);
--显示学生列表数据
procedure p_list_infos;
--增加一条学生记录
procedure p_add_infos(
p_stuid infos.stuid%type,
p_stuname infos.stuname%type,
p_gender infos.gender%type,
p_age infos.age%type,
p_seat infos.seat%type,
p_enrolldate infos.enrolldate%type,
p_stuaddress infos.stuaddress%type,
p_classno infos.classno%type);
--删除一条学生记录
procedure p_delete_infos(p_stuid infos.stuid%type);
--根据stuid修改学生的姓名
procedure p_edit_infos_name(
p_stuid infos.stuid%type,
p_stuname infos.stuname%type);
end;
(2)创建包体
create or replace package body pck_infos
as
v_msg varchar2(100); --show message
v_max_age number; --max age
v_min_age number; --min age --判断学生是否存在
function f_exist_infos(p_stuid infos.stuid%type)
return boolean; --show msg
procedure p_show_msg; --初始化操作
procedure p_init(p_max number, p_min number)
as
begin
select count(stuid) into v_infos_count from infos;
v_max_age:=p_max;
v_min_age:=p_min;
v_msg:='init finished!';
p_show_msg;
end p_init; --显示信息
procedure p_show_msg
as
begin
dbms_output.put_line(v_msg);
end p_show_msg;
--判断学生是否存在
function f_exist_infos(p_stuid infos.stuid%type)
return boolean
as
v_num number;
begin
select count(stuid) into v_num from infos where stuid=p_stuid;
if v_num=1 then
return true;
else
return false;
end if;
end f_exist_infos; --显示学生列表数据
procedure p_list_infos
as
v_infos_record infos%rowtype;
cursor cur_infos is select * from infos;
begin
open cur_infos;
loop
fetch cur_infos into v_infos_record;
exit when cur_infos%notfound;
dbms_output.put_line('stuid:'||v_infos_record.stuid);
end loop;
close cur_infos;
end p_list_infos; --增加一条学生记录
procedure p_add_infos(
p_stuid infos.stuid%type,
p_stuname infos.stuname%type,
p_gender infos.gender%type,
p_age infos.age%type,
p_seat infos.seat%type,
p_enrolldate infos.enrolldate%type,
p_stuaddress infos.stuaddress%type,
p_classno infos.classno%type)
as
begin
if not f_exist_infos(p_stuid) then
insert into infos(stuid,stuname,gender,age,seat,enrolldate,stuaddress,classno)
values(p_stuid,p_stuname,p_gender,p_age,p_seat,p_enrolldate,p_stuaddress,p_classno);
commit;
v_infos_count:=v_infos_count+1;
else
v_msg:='already exist!';
end if;
end p_add_infos; --删除一条学生记录
procedure p_delete_infos(p_stuid infos.stuid%type)
as
begin
if f_exist_infos(p_stuid) then
delete from infos where stuid=p_stuid;
commit;
v_infos_count:=v_infos_count-1;
else
v_msg:='not exist infos!';
end if;
end p_delete_infos; --根据stuid修改学生的姓名
procedure p_edit_infos_name(
p_stuid infos.stuid%type,
p_stuname infos.stuname%type)
as
begin
if f_exist_infos(p_stuid) then
update infos set stuname=p_stuname where stuid=p_stuid;
commit;
else
v_msg:='not exists infos';
end if;
end p_edit_infos_name; end;

Oracle 中包的应用的更多相关文章

  1. Oracle 中包(Package)

    一.什么要使用包?        在一个大型项目中,可能有很多模块,而每个模块又有自己的过程.函数等.而这些过程.函数默认是放在一起的(如在PL/SQL中,过程默认都是放在一起 的,即Procedur ...

  2. Oracle中包的创建

    包是过程和函数的集合体,包包括创建包和创建包体,创建包的时候在可以定义过程和函数,包体中则具体实现过程和函数. eg: --创建包 create  or replace package mypac1 ...

  3. java实现调用ORACLE中的游标和包

    今天把oracle中的包和游标学习了下,不废话,网上的的有些代码是错误的,抄来抄去,就自己实践了下,做个记录.直接上图,上代码 通过plsql创建自己的的包,包分为包头和包体. 1.包头如下: CRE ...

  4. Oracle中如何导出存储过程、函数、包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句?

    Oracle中如何导出存储过程.函数.包和触发器的定义语句?如何导出表的结构?如何导出索引的创建语句? QQ群里有人问:如何导出一个用户下的存储过程?   麦苗答:方法有多种,可以使用DBMS_MET ...

  5. Oracle中DBMS_LOB包使用小结

    本文主要介绍oracle数据库中dbms_lob包的使用以及使用dbms_lob包来维护lob数据库类型的基本方法.随着社会的发展,在现代信息系统的开发中,需要存储的已不仅仅是简单的文字信息,同时还包 ...

  6. oracle中utl_file包读写文件操作实例学习

    在oracle中utl_file包提供了一些操作文本文件的函数和过程,学习了一下他的基本操作 1.创建directory,并给用户授权 复制代码 代码如下: --创建directory create ...

  7. oracle中的创建过程,函数,包

    一.创建存储过程 存储过程是在oracle中存取完成特定业务逻辑的代码块.存储过程是命名块,匿名块不存在数据库中,命名块会存储到数据库中,匿名块每次运行都需要提前编译,命名块一次存储,只会编译一次.命 ...

  8. Oracle中生成随机数的函数(转载)

    在Oracle中的DBMS_RANDOM程序包中封装了一些生成随机数和随机字符串的函数,其中常用的有以下两个: DBMS_RANDOM.VALUE函数 该函数用来产生一个随机数,有两种用法: 1. 产 ...

  9. oracle中函数和存储过程的区别和联系【转载竹沥半夏】

    oracle中函数和存储过程的区别和联系[转载竹沥半夏] 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,但也有自己的特点.刚学完函数和存储过程,下面来和大家分享一下自己 ...

随机推荐

  1. Ubuntu 下安装使用文件比较合并图形工具Meld

    Meld是一款跨平台的文件比较合并工具使用Python开发,具体内容参照官网:http://meldmerge.org/ 注意以下环境要求: Requirements Python 2.7 (Pyth ...

  2. LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别

    LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和作用分别如下: LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径 ...

  3. MapReduce流程、如何统计任务数目以及Partitioner

    核心功能描述 应用程序通常会通过提供map和reduce来实现 Mapper和Reducer接口,它们组成作业的核心. Map是一类将输入记录集转换为中间格式记录集的独立任务. 这种转换的中间格式记录 ...

  4. JAVA并发实现三(线程的挂起和恢复)

    package com.subject01; /** * 通过标识位,实现线程的挂起和回复 * com.subject01.AlternateSuspendResume.java * @author ...

  5. [原创]Web前端开发——让ie 7 8支持表单的placeholder属性

    今天在写页面的时候,测试低版本浏览器时,发现input写的placeholder显示的是空白,所以特意写了一个普遍试用的方法来让低版本浏览器支持这个属性. 博主建了一个技术共享qq群:,因为目前人数还 ...

  6. Sonar入门(四):Eclipse集成Sonar

    sonar及其插件在项目中的使用方法 Sonar平台 Sonar平台的安装见一文, 在Sonar平台上进行的检查可以通过hudson进行触发, A. 没有做持续集成的项目可以复制以下hudson上的任 ...

  7. c++ 11 多线程教学(1)

    本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...

  8. 破解Veeam过程

    1)运行Veeam_Backup_Setup.exe,但是不要继续下一步: 2)进入到%temp%\IXP000.TMP目录,例如windows xp sp3环境默认为C:\Documents and ...

  9. 查看linux版本和内核信息

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/versionLinux version 2.6.32 ...

  10. keepalived vip漂移基本原理及选举算法

    keepalived可以将多个无状态的单点通过虚拟IP(以下称为VIP)漂移的方式搭建成一个高可用服务,常用组合比如 keepalived+nginx,lvs,haproxy和memcached等.它 ...