--将方法和过程用包定义

create or replace package pkg_emp

as

       --输入员工编号查询出员工信息

       procedure pro_findInfo(

         in_empno emp2.empno%type,

         out_name out emp2.ename%type,

         out_sal out emp2.sal%type          

       );

       --根据部门编号修改本部门员工工资

       procedure pro_editInfo(

         in_emp_record emp2%rowtype,

         out_flag out boolean

       );

       --输入新员工信息并保存到数据库

       procedure pro_addInfo(

         in_emp_new_record emp2%rowtype

       );

       --统计工资信息

       function fun_sum(

         num_a number,

         num_b number

       ) return number;

end pkg_emp;

 

--实现包

create or replace package body pkg_emp

as

       --输入员工编号查询出员工信息

       procedure pro_findInfo(

         in_empno emp2.empno%type,

         out_name out emp2.ename%type,

         out_sal out emp2.sal%type          

       )

       as

       begin

         select ename, sal into out_name, out_sal from emp2 where empno = in_empno;

       end pro_findInfo;

       

       --根据部门编号修改本部门员工工资

       procedure pro_editInfo(

         in_emp_record emp2%rowtype,

         out_flag out boolean

       )

       is         

       begin

         update emp2 set sal = in_emp_record.sal where deptno = in_emp_record.deptno;

         out_flag := true;

         /*exception

           when no_data_found then

             out_flag := false;

         commit;*/

         if (sql%rowcount < 1) then

           out_flag := false;

         else

           out_flag := true;

           commit;

         end if;

       end pro_editInfo;

       

       --输入新员工信息并保存到数据库

       procedure pro_addInfo(

         in_emp_new_record emp2%rowtype

       )

       as

         temp_sql varchar2(200);

       begin

         temp_sql := 'insert into emp2(empno, ename, sal, comm, deptno) values(:1, :2, :3, :4, :5)';

         execute immediate temp_sql using in_emp_new_record.empno, in_emp_new_record.ename,

               in_emp_new_record.sal, in_emp_new_record.comm, in_emp_new_record.deptno;

         commit;

       end;

       

       --统计工资信息

       function fun_sum(

         num_a number,

         num_b number

       ) return number

       is

       begin

         return num_a + num_b;

       end fun_sum;

end pkg_emp;

       

 

--测试1

declare

       out_name emp2.ename%type;

       out_sal emp2.sal%type;

begin

     pkg_emp.pro_findInfo(7369, out_name, out_sal);

     dbms_output.put_line(out_name);

     dbms_output.put_line(out_sal);

end;  

 

--测试2

select * from emp2;

declare

       in_emp_record emp2%rowtype;

       flag boolean;

begin

     in_emp_record.deptno := &部门编号;

     in_emp_record.sal := &员工工资;

     pkg_emp.pro_editInfo(in_emp_record, flag);

     if (flag = false) then

       dbms_output.put_line('no');

     else

       dbms_output.put_line('yes');

     end if;

end;   

 

--测试3

declare

       new_emp_record emp2%rowtype;

begin

     new_emp_record.empno := &员工编号;

     new_emp_record.ename := &姓名;

     new_emp_record.sal := &工资;

     new_emp_record.comm := &奖金;

     new_emp_record.deptno := &部门编号;

     pkg_emp.pro_addInfo(new_emp_record);

end;

       

--测试4

declare

     sum_emp number;  

begin

     select pkg_emp.fun_sum(sal, nvl(comm, 0)) into sum_emp from emp2

     where empno = &员工编号;

     dbms_output.put_line('员工总工资:' || sum_emp);

end;

Oracle笔记 十三、PL/SQL面向对象之package的更多相关文章

  1. Oracle数据库之PL/SQL包

    Oracle数据库之PL/SQL包 1. 简介 包(PACKAGE)是一种数据对象,它是一组相关过程.函数.变量.常量和游标等PL/SQL程序设计元素的组合,作为一个完整的单元存储在数据库中,用名称来 ...

  2. oracle instantclient basic +pl/sql 安装和配置

    oracle instantclient basic +pl/sql 安装和配置 大家都知道,用PL/SQL连接Oracle,是需要安装Oracle客户端软件的,oracle客户端有点大,比较耗资源. ...

  3. oracle系列(四)PL/SQL

    过程,函数,触发器是PL/SQL编写的,存储在oracle中的.PL/SQL是非常强大的数据库过程语言. PL/SQL优点:性能,模块化,网络传输量,安全性缺点:移植性不好 简单分类:块:过程,函数, ...

  4. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》

    本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...

  5. 《oracle每日一练》免安装Oracle客户端使用PL/SQL

    免安装Oracle客户端使用PL/SQL Oracle客户端挺招人烦的,部署连接它的应用通常需要先安装它的客户端,安装程序要求在目标机器上写注册表,假设你没有洁癖的话,你仍可能被下面的事情绊住:当你的 ...

  6. Oracle 客户端安装 + pl/sql工具安装配置

    Oracle 客户端安装 +  pl/sql工具安装配置 下载oracle客户端,并在本地安装. 11g下载地址为: http://www.oracle.com/technetwork/databas ...

  7. Oracle数据库之PL/SQL触发器

    Oracle数据库之PL/SQL触发器 1. 介绍 触发器(trigger)是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是 ...

  8. Oracle数据库之PL/SQL过程与函数

    Oracle数据库之PL/SQL过程与函数 PL/SQL块分为匿名块与命名块,命名块又包含子程序.包和触发器. 过程和函数统称为PL/SQL子程序,我们可以将商业逻辑.企业规则写成过程或函数保存到数据 ...

  9. Oracle数据库之PL/SQL异常处理

    Oracle数据库之PL/SQL异常处理 异常指的是在程序运行过程中发生的异常事件,通常是由硬件问题或者程序设计问题所导致的. PL/SQL程序设计过程中,即使是写得最好的程序也可能会遇到错误或未预料 ...

随机推荐

  1. SOA_环境安装系列4_Oracle SOA安装和环境搭建(案例)

    2015-01-02 Created By BaoXinjian

  2. HDU 5428 The Factor

    话说这题意真的是好难懂啊,尽管搜到了中文题意,然而还是没懂,最后看到了一个题解才懂的.http://www.cnblogs.com/Apro/p/4784808.html#3470972 题意:给出n ...

  3. NeHe OpenGL教程 第四十四课:3D光晕

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. ylbtech-LanguageSamples-Yield

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Yield 1.A,示例(Sample) 返回顶部 “Yield”示例 本示例演示如何创 ...

  5. Python从2.6升级到2.7,使用pip安装module,报错:No Module named pip.log(转载)

    From:http://blog.csdn.net/iefreer/article/details/8086834 python升级后,使用pip安装module,错误: 错误原因:版本升级后,之前的 ...

  6. SPREAD_NET6

    SPREAD_NET6 下载地址 http://www.gcpowertools.com.cn/downloads/trial/Spread.NET/EN_SPREAD_NET6_SETUP_RA_6 ...

  7. c# winform快捷键设置

    设置 Form 的 KeyPreview=true 然后在Form 的案件事件里判断按钮类型进行分别调用就可以了 private void Form1_KeyDown(object sender, K ...

  8. Replace JSON.NET with ServiceStack.Text in ASP.NET Web API

    Because ServiceStack.Text performs much better I recently stumbled across a comparison of JSON seria ...

  9. 日期选择控件-laydate

    laydate控件非常简单易用,只需要调用一个个函数就可以轻松实现日期时间选择. <%@ page language="java" import="java.uti ...

  10. nyoj 97 兄弟郊游问题

    点击打开链接 兄弟郊游问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 兄弟俩骑车郊游,弟弟先出发,每分钟X米,M分钟后,哥哥带一条狗出发.以每分钟Y米的速度去追弟弟 ...