设为首页 收藏本站
查看: 1882|回复: 0

[经验分享] oracle pl/sql之包(package)

[复制链接]

尚未签到

发表于 2018-9-12 09:38:22 | 显示全部楼层 |阅读模式
  oracle_package.sql
  package:简化应用程序设计,提高应用性能,而且可以实现信息隐藏,子程序重载等功能。
  包用于逻辑组合相关的PL/SQL types, variables, and subprograms。它由包头(package specification)和包体(package body)两部分组成。首先要建立包头,再建立包体。
  包头只用来定义公共组件。
  ---包头语法
  create or replace package pkg_name
  is|as
  public type and item declarations
  subprogram specifications
  end package_name;
  Specify the package specification, which can contain type definitions, cursor declarations, variable declarations, constant declarations, exception declarations, PL/SQL subprogram specifications, and call specifications, which are declarations of a C or Java routine expressed in PL/SQL.
  ---包体语法
  create or replace package body pkg_name
  is|as
  private type and item declarations
  subprogram bodies
  end pkg_name;
  需求:
  1.用过程来实现添加员工信息(empno,ename,sal,deptno)
  2.用过程来实现删除某个员工
  3.用函数来查询某个员工的薪水
  Error(8,11): PLS-00323: subprogram or cursor 'PRO_DELETE_EMPLOYEE' is declared in a package specification and must be defined in the package body
  1.包的建立
  create or replace package body pkg_emp is
  function fun_valid_deptno(v_deptno number) return boolean
  is
  v_tmp number;
  begin
  select 1 into v_tmp from dept where deptno=v_deptno;
  return true;
  exception
  when no_data_found then
  return false;
  end;
  procedure pro_add_emp(v_empno number,v_ename varchar2,v_salary number,v_deptno number default g_deptno)
  is
  begin
  if fun_valid_deptno(v_deptno) then
  insert into emp(empno,ename,sal,deptno) values(v_empno,v_ename,v_salary,v_deptno);
  else
  raise_application_error(-20001,'deptno not exist');
  end if;
  exception
  when dup_val_on_index then
  raise_application_error(-20002,'empno is already exist');
  end;
  procedure pro_delete_employee(v_empno number)
  is
  begin
  delete from emp where empno=v_empno;
  if sql%notfound then
  raise_application_error(-20003,'empno is not exist.');
  end if;
  end;
  function fun_get_sal(v_empno number) return number
  is
  v_sal number;
  begin
  select sal into v_sal from emp where empno=v_empno;
  return v_sal;
  exception
  when others then
  raise_application_error(-20003,'empno is not exist');
  end;
  end pkg_emp;
  2.包的重载特性
  --------------------------------------------------------
  ---package code
  create or replace package pkg_emp is
  g_deptno number :=20;
  procedure pro_add_emp(v_empno number,v_ename varchar2,v_salary number,
  v_deptno number default g_deptno );
  procedure pro_delete_employee(v_empno number);
  procedure pro_delete_employee(v_ename varchar2);
  function fun_get_sal(v_empno number) return number;
  function fun_get_sal(v_ename varchar2) return number;
  end pkg_emp;
  --------------------------------------------------------
  ---package body code
  --------------------------------------------------------
  create or replace package  body pkg_emp is
  function fun_valid_deptno(v_deptno number) return boolean is
  v_tmp number;
  begin
  select 1 into v_tmp from dept where deptno=v_deptno;
  return true;
  exception when no_data_found then
  return false;
  end;
  procedure pro_add_emp(v_empno number,v_ename varchar2,v_salary number,
  v_deptno number default g_deptno )
  is
  begin
  if fun_valid_deptno(v_deptno) then
  INSERT INTO emp(empno,ename,sal,deptno) values(v_empno,v_ename,v_salary,v_deptno);
  else
  raise_application_error(-20001,'deptno is not exist.');
  end if;
  exception when dup_val_on_index then
  raise_application_error(-20002,'empno is exist.');
  end;
  procedure pro_delete_employee(v_empno number)
  is
  begin
  delete from emp where empno=v_empno;
  if sql%notfound then
  raise_application_error(-20003,'empno is not exist.');
  end if;
  end;
  procedure pro_delete_employee(v_ename varchar2)
  is
  begin
  delete from emp where ename=v_ename;
  if sql%notfound then
  raise_application_error(-20003,'ename is not exist.');
  end if;
  end;
  function fun_get_sal(v_empno number) return number
  is
  v_sal emp.sal%type;
  begin
  select sal into v_sal from emp where empno=v_empno;
  return v_sal;
  exception
  when  others then
  raise_application_error(-20003,'empno is not exist.');
  end;
  function fun_get_sal(v_ename varchar2) return number
  is
  v_sal emp.sal%type;
  begin
  select sal into v_sal from emp where ename=v_ename;
  return v_sal;
  exception
  when  others then
  raise_application_error(-20003,'ename is not exist.');
  end;
  end pkg_emp;
  --------------------------------------------------------
  3.包的构造过程
  在包中定义全局变量后,需要初始化全局变量。此时可以使用包构造过程,包的构造过程没有任何名称,它是在包体中实现了包的其他子程序之后,以BEGIN开始,以END结束.
  ---------------------------------------------------
  ---package code
  create or replace PACKAGE pkg_sal is
  v_minsal number(6,2);
  v_maxsal number(6,2);
  procedure pro_update_sal(v_sal number,v_empno number);
  procedure pro_update_sal(v_sal number,v_ename varchar2);
  end pkg_sal;
  -----------------------------------------------
  ---package body code
  create or replace PACKAGE body pkg_sal is
  procedure pro_update_sal(v_sal number, v_empno number) is
  begin
  if v_sal between v_minsal and v_maxsal then
  update emp set sal = v_sal where empno = v_empno;
  if sql%notfound then
  raise_application_error(-20001, 'empno is not exist.');
  end if;
  else
  raise_application_error(-20001, 'salary is not range.');
  end if;
  end;
  procedure pro_update_sal(v_sal number, v_ename varchar2)
  is
  begin
  if v_sal between v_minsal and v_maxsal then
  update emp set sal = v_sal where ename = v_ename;
  if sql%notfound then
  raise_application_error(-20002, 'ename is not exist.');
  end if;
  else
  raise_application_error(-20001, 'salary is not range.');
  end if;
  end;
  begin
  select min(sal), max(sal) into v_minsal, v_maxsal from emp;
  end pkg_sal;
  ---------------------------------------------------------------


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-573192-1-1.html 上篇帖子: oracle pl/sql之函数(function) 下篇帖子: oracle pl/sql之触发器(trigger)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表