----程序包
--案例09:程序包试验1
/*步骤1:新建包*/
CREATE OR REPLACE PACKAGE pack_me IS
PROCEDURE order_proc (orno VARCHAR2);
FUNCTION order_fun(ornos VARCHAR2) RETURN VARCHAR2;
END pack_me;
/*步骤2:新建包主体*/
CREATE OR REPLACE PACKAGE BODY pack_me AS
PROCEDURE order_proc (orno VARCHAR2) IS
stat CHAR(1);
BEGIN
SELECT ostatus INTO stat FROM order_master
WHERE orderno = orno;
IF stat = 'p' THEN
DBMS_OUTPUT.PUT_LINE('暂挂的订单');
ELSE
DBMS_OUTPUT.PUT_LINE('已完成的订单');
END IF;
END order_proc;
FUNCTION order_fun(ornos VARCHAR2)
RETURN VARCHAR2
IS
icode VARCHAR2(5);
ocode VARCHAR2(5);
qtyord NUMBER;
qtydeld NUMBER;
BEGIN
SELECT qty_ord, qty_deld, itemcode, orderno
INTO qtyord, qtydeld, icode, ocode
FROM order_detail
WHERE orderno = ornos;
IF qtyord < qtydeld THEN
RETURN ocode;
ELSE
RETURN icode;
END IF;
END order_fun;
END pack_me;
/
--步骤3:执行包中的过程
execute pack_me.order_proc('o002');
--步骤4:执行包中的函数
declare
msg varchar2(10);
begin
msg:=pack_me.order_fun('o002');
dbms_output.put_line('值是 :' ||msg);
end;
--案例10:程序包试验2
/*步骤1:新建包*/
create or replace package studentpackage
is
type currefstudent is ref cursor return student%rowtype;
procedure selectstudent(findid in student.stuid%type);
procedure insertstudent(newstudent in student%rowtype);
procedure updatestudent(newstudent in student%rowtype);
procedure deletestudent(delid in student.stuid%type);
procedure returnstudent (inoutstu in out currefstudent);
function returnrecordcount return number;
end studentpackage;
/*步骤2:新建包体*/
create or replace package body studentpackage as
procedure selectstudent(findid in student.stuid%type) as
cursor findcur is select * from student where stuid=findid;
begin
for s in findcur loop
dbms_output.put_line(s.stuid||' '||s.stuname|| ' '||s.sex);
end loop;
exception
when no_data_found then
dbms_output.put_line('没有查到ID为:' ||findid|| '的记录!!');
when others then
dbms_output.put_line('查询过程遇到不可预知的错误!');
end selectstudent;
procedure insertstudent(newstudent in student%rowtype)as
irec integer;
not_exists_student exception; --自定义异常错误
begin
select count(*) into irec from student where stuid=newstudent.stuid;
if irec>0 then
raise not_exists_student;
else
insert into student values (newstudent.stuid, newstudent.stuname, newstudent.sex);
commit;
end if;
exception
when not_exists_student then
dbms_output.put_line ('要插入的编号为:'||newstudent.stuid||'的记录已经存在!');
when others then
dbms_output.put_line('插入记录操作的过程中出现不可预知的错误!');
end insertstudent;
procedure updatestudent(newstudent in student%rowtype) as
irec integer;
begin
select count(*) into irec from student where stuid=newstudent.stuid;
if irec=0 then
dbms_output.put_line('编号为:'||newstudent.stuid || '的记录不存在,修改失败!' );
else
update student set stuname=newstudent.stuname, sex=newstudent.sex
where stuid=newstudent.stuid;
commit;
dbms_output.put_line('修改操作成功!');
end if;
exception
when no_data_found then
dbms_output.put_line('编号为:' ||newstudent.stuid||'的记录不存在,修改失败!');
when others then
dbms_output.put_line('执行修改操作时发生不可预知的错误!修改不成功!');
end updatestudent;
procedure deletestudent (delid in student.stuid%type) as
irec integer;
begin
select count(*) into irec from student where stuid=delid;
if irec=0 then
dbms_output.put_line('编号为:'||delid ||'的记录不存在,删除不成功!');
else
delete from student where stuid=delid;
commit;
dbms_output.put_line('删除成功!恭喜');
end if;
exception
when others then
dbms_output.put_line('执行删除时发生不可预知的错误,未能按要求执行!');
end deletestudent;
procedure returnstudent(inoutstu in out currefstudent) as
begin
open inoutstu for select * from student;
end returnstudent;
function returnrecordcount return number as
reccount number(10);
begin
select count(*) into reccount from student;
return reccount;
exception
when others then
dbms_output.put_line ('查询记录发生不可预知的错误!');
end returnrecordcount;
end studentpackage;
/*步骤3:调用存储过程*/
--1.调用studentpackage中的insertstudent过程
declare
newstu student%rowtype;
begin
newstu.stuid:='1001';
newstu.stuname:='马大哈';
newstu.sex:='男';
studentpackage.insertstudent(newstu);
end;
--2.调用studentpackage中的updatestudent过程
declare
newstu student%rowtype;
begin
newstu.stuid:='1001';
newstu.stuname:='马大哈';
newstu.sex:='女';
studentpackage.updatestudent(newstu);
exception
when dup_val_on_index then
dbms_output.put_line('唯一约束被破坏!');
when others then
dbms_output.put_line('更新过程出现不可预知的错误!');
end;
--3.调用studentpackage中的deletestudent过程
begin
studentpackage.deletestudent('888');
end;
--4.studentpackage中的函数过程
begin
dbms_output.put_line(studentpackage.returnrecordcount);
end;
--案例11:函数的限制
/*步骤1:新建测试表*/
create table sz
(a int);
/*步骤2:新建包*/
create or replace package mypack
as
procedure updatetable(s int);
pragma restrict_references(updatetable, wnds);
end;
/*步骤3:新建包体*/
create or replace package body mypack
as
procedure updatetable(s int) is
begin
update sz set a=s;
end;
end;
分享到:
相关推荐
老二牛车教育程矢第七章上机课子程序和程序包.pdf
数据段D_SEG包括X和Y数组,数据段E-SEG包含R数组 ,实现R<-X+Y-3,COMPUTE 主程序和子程序在同一模块的不同代码段中
金蝶K/3 wise电子看板程序包-包含操作手册以及安卓版程序
oracle子程序和程序包(主要对oracle存储过程和函数作了讲解),里面有详细的例子。
C51单片机 项目2-电子日历策程序包和仿真文件 (仿真文件+程序包)C51单片机 项目2-电子日历策程序包和仿真文件 (仿真文件+程序包)C51单片机 项目2-电子日历策程序包和仿真文件 (仿真文件+程序包)C51单片机 项目...
S7-200 SMART200 项目密码 POU子程序块密码 4种解密软件 SMART200电脑上程序.smart 文件 S7-200电脑上程序mwp文件 第1 S7-200 POU子程序块密码解密 第2 S7-200 项目密码解密 第3 SMART200 POU子程序块密码解密 第4...
该资源系本人培训期间的关于ORCL 子程序和程序包所有实用案例,在此共享希望对大家学习有所帮助
PIC单片机常用子程序包,对学习pic单片机的朋友很有的
ls dyna子程序,Fortran调用计算
这是我收集的很实用的PIC单片机的子程序包括:PIC12F629控制七彩灯、PIC16F84模拟6221红外发射、PIC16F876驱动诺基亚5110LCD、PIC16F877串行通信程序、PIC单片机I-O口单线通讯程序、PIC单片机读写时钟芯片DS1302汇编...
MCS-51单片机实用子程序库 子程序库的使用方法如下: 1.将子程序库全部内容链接在应用程序之后,统一编译即可。优点是简单方便,缺点是程序太长,大量无关子程序也包含在其中。 2.仅将子程序库中的有关部分...
Oracle数据库子程序和程序包PPT教案.pptx
S7-200SMART标准化程序(含MODBUS RTU轮询+可重复调用子程序+注释说明使用方法)
S7-smart200 PLC开源标准化程序(包含可重复调用的定时器和计数器)
Oracle 子程序and程序包的基础知识与技术结合具。
目前已有若干版本的子程序库公开发表,它们各有特色。本程序库中的开平方算法为快速逼近算法,它能达到牛顿迭代法同样的精度,而速度加快二十倍左右,超过双字节定点除法的速度。 1.将子程序库全部内容链接在...
该程序用于降维和特征提取。程序包含主程序和子程序,程序已调通
老二牛车教育第七章理论课子程序和程序包.pdf
oracle视频教程(游标、子程序、包) oracle视频教程(游标、子程序、包)
通用51子程序,随时调用(包含单片机所有的常见模块程序),学单片机真的不难!