--创建一个函数,用来根据部门编号返回调薪幅度
create or replace function get_ratio_by_dept(deptno varchar2)
return number is
n_salaryratio number(10,2); --调薪比率返回值变量
begin
case deptno
when 10 then
n_salaryratio:=1.09;
when 20 then
n_salaryratio:=1.11;
when 30 then
n_salaryratio:=1.18;
else
n_salaryratio:=0;
end case;
return n_salaryratio;
end; begin
dbms_output.put_line(get_ratio_by_dept(20));
end; --创建一个存储过程,用来实现加薪,它将调用get_ratio_by_dept来获取加薪幅度 create or replace procedure raise_salary(p_empno number)
as
v_deptno number(2);
v_ratio number(10,2); --存储调薪幅度变量
begin
select deptno into v_deptno from emp where empno=p_empno;
v_ratio:=get_ratio_by_dept(v_deptno);
if v_ratio>0
then
update scott.emp
set sal =sal* (1+v_ratio)
where empno = p_empno;
end if;
dbms_output.put_line('加薪成功!');
exception
when no_data_found then
dbms_output.put_line('没有找到该员工的任何信息!');
when others then
dbms_output.put_line('调整薪资时出现了错误!');
end; set serveroutput on;
exec raise_salary(7369); --创建包头
create or replace package emp_sal_pkg as
function get_ratio_by_dept(deptno varchar2) return number;
procedure raise_salary(p_empno number);
end emp_sal_pkg; --创建包体
create or replace package body emp_sal_pkg as
function get_ratio_by_dept(deptno varchar2)
return number
is
n_salaryratio number(10,2);
begin
case deptno
when 10 then
n_salaryratio:=1.09;
when 20 then
n_salaryratio:=1.11;
when 30 then
n_salaryratio:=1.18;
else
n_salaryratio:=1;
end case;
return n_salaryratio;
end get_ratio_by_dept; procedure raise_salary (p_empno number)
as
v_deptno number(2);
v_ratio number(10,2);
begin
select deptno into v_deptno from emp where empno=p_empno;
v_ratio:=get_ratio_by_dept(v_deptno);
if v_ratio>0
then
update scott.emp
set sal =sal* (1+v_ratio)
where empno = p_empno;
end if;
dbms_output.put_line('加薪成功!');
exception
when no_data_found then
dbms_output.put_line('没有找到该员工的任何信息!');
when others then
dbms_output.put_line('调整薪资时出现了错误!');
end raise_salary;
end emp_sal_pkg;
--创建过程添加新员工
create or replace procedure AddNewEmp(p_empno emp.empno%type,
p_ename emp.ename%type,
p_job emp.job%type,
p_sal emp.sal%type,
p_deptno emp.deptno%type:=20)
as
begin
if p_empno<0 then
raise_application_error(-20001,'员工编号必须大于0');
end if;
insert into emp
(empno,ename,job,sal,deptno)
values (p_empno,p_ename,p_job,p_sal,p_deptno);
end AddNewEmp; begin
AddNewEmp(8236,'诸葛亮','策划人员',25000,40);
end; 子程序创建高度可维护,重用的代码
(1)模块化代码(2)简化应用程序(3)提升管理性:需求变更,只要改子程序
(4)可重用性,提高性能
程序调试
C+R 直接运行 C+N单击进入 C+O单步逃过 C+T单步退出
--在过程中使用return语句
create or replace procedure RaiseSalary(
p_empno emp.empno%type) as
v_job emp.job%type;
v_sal emp.sal%type;
begin
select job,sal into v_job,v_sal from emp
where empno=p_empno;
if v_job<>'CLERK' then
return; --如果不是职员则退出
elsif v_sal>3000 then
return ;
else
--否则更新薪资记录
update emp set sal=Round(sal*1.12,2) where
empno=p_empno;
end if;
exception
when no_data_found then
dbms_output.put_line('没有找到员工记录');
end RaiseSalary;
begin
RaiseSalary(7369);
end;
select * from emp where empno=7369;
--查询当前scott方案下的过程和函数列表
select object_name,object_type,created,
last_ddl_time,status,temporary
from user_objects
where object_type in ('PROCEDURE','FUNCTION','TABLE'); --in out 模式参数
create or replace procedure calcRaiseSalary(
p_job in varchar2,
p_salary in out number
) as
v_sal number(10,2);
v_job varchar2(10);
begin
if p_job='CLERK' then
v_sal:=p_salary*1.12; elsif p_job='销售人员' then
v_sal:=p_salary*1.18;
elsif p_job='经理' then
v_sal:=p_salary*1.19;
else
v_sal:=p_salary; end if;
p_salary:=v_sal; ---值传递 end calcRaiseSalary; declare
v_sal number(10,2);
v_job varchar2(10);
begin
select sal,job into v_sal,v_job from emp where
empno=7369;
calcRaiseSalary(v_job,v_sal);
dbms_output.put_line('计算后的调整薪水为:'||v_sal); --||'JOB'||v_jo
end; select * from emp where empno=7369; 引用传递:将实际参数的指针(内存地址)传递给形式参数,
值传递:将实际参数的值赋给形式参数,值拷贝,指向不同的内存地址 如果参数是大型数据结构,比如集合,记录和对象实例, 参数复制会大大降低执行速度,
消耗内存
nocopy 使得out和in out模式的参数按引用进行传递 --编译包规范
alter package emp_pkg_overloading compile specification;
--编译包体
alter package emp_pkg_overloading compile body;
--同时编译包规范和包体
alter package emp_pkg_overloading compile package; --查询包规范和包体信息
select object_name,object_type,created,last_ddl_time from user_objects
where object_type in ('PACKAGE','PACKAGE BODY'); --查看包的源代码
select line,text from user_source where name='EMP_MGMT_PKG_OVERLOADING'
and type='PACKAGE'; --使用returning into 为记录变量赋值
declare
type t_emp is record(
empno emp.empno%type,
ename emp.ename%type,
sal emp.sal%type
);
emp_info t_emp;
old_sal emp.sal%type;
begin
select sal into old_sal
from emp
where empno=7369;
update emp
set sal=sal*1.1
where empno=7369
returning empno,ename,sal into emp_info;
dbms_output.put_line(
emp_info.empno||' '||emp_info.ename ||' '||
old_sal||' '|| emp_info.sal); end;
select sal,ename from emp where empno =7369 --在insert语句中使用记录类型
declare
type t_dept_rec is record(
rec_deptno number,
rec_dname varchar2(14),
rec_loc varchar2(13)
);
rec_dept_1 t_dept_rec;
rec_dept_2 dept%rowtype;
begin
rec_dept_1.rec_deptno:=71;
rec_dept_1.rec_dname:='系统部';
rec_dept_1.rec_loc:='上海';
insert into dept values rec_dept_1;
rec_dept_2.deptno:=72;
rec_dept_2.dname:='开发部';
rec_dept_2.loc:='重庆';
insert into dept values rec_dept_2;
end; select * from dept --在update语句中使用记录类型
declare
rec_dept_2 dept%rowtype;
begin
rec_dept_2.deptno:=20;
rec_dept_2.dname:='系统部';
rec_dept_2.loc:='上海';
update dept set row=rec_dept_2 where deptno=rec_dept_2.deptno;
end; 使用记录类型的限制
(1)记录类型不能出现在select语句的选择列表, where子句,group by 子句
或order by子句中
(2)update语句中Row关键字只能出现在set语句之后,并且不能和子查询连用
(3)不能包含其他的变量和值,不能具有嵌套的记录类型,不支持在
execute immediate 语句中使用记录类型.

PLSQL函数,存储过程的更多相关文章

  1. mysqldump导出--数据+结构+(函数+存储过程)

    #导出某个数据库--结构+数据shell>mysqldump -h192.168.161.124 -uroot -pxxxxxx --opt db_name |gzip -9 > /db_ ...

  2. plsql调试存储过程卡住的原因以及处理

    用PLSQL调试存储过程的时候,经常会遇到这个的情况,点调试后,继续点单步都是灰色,想停下来,但是取消也要点很多次才能取消掉. 就像下面的情况: 一直以为是个BUG,直到最近有人告诉我了真相. 出现这 ...

  3. Java -- JDBC 学习--调用函数&存储过程

    调用函数&存储过程 /** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connec ...

  4. PL/SQL轻量版(四)——存储函数/存储过程与触发器

    概述 ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数.过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL 块,均存储在数 ...

  5. ql 判断 函数 存储过程是否存在的方法

    下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助. 库是否存在 if exists(select * from master..s ...

  6. sql 判断 函数 存储过程是否存在的方法

    下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助.库是否存在if exists(select * from master..sys ...

  7. mysql 查询表,视图,触发器,函数,存储过程

    1. mysql查询所有表: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '数据库名' AND  TAB ...

  8. MySQL 5.6 主从复制如何处理——触发器,函数,存储过程,调度事件

      截图来自MySQL5.6的pdf版文档. 说明: 1)基于语句的复制时,trigger会在slave上执行,所以slave上也需要有trigger的定义,不然会导致主从数据不一致的: 2)基于行的 ...

  9. 基本开题的感觉是了-MySQL继续继续(自定义函数&存储过程)

    hi 感觉论文开题基本确定了,凯森 1.MySQL -----自定义函数----- ----基本 两个必要条件:参数和返回值(两者没有必然联系,参数不一定有,返回一定有) 函数体:合法的SQL语句:以 ...

随机推荐

  1. AOP的核心:代理与织入

    分为两步: 1.动态生成代理类: 2.织入: 2.6 织入(Weaving) 织入是将增强添加到目标的具体连接点上的过程 . AOP 织入方式: 方式 实现 应用编译期织入 特殊的 Java 编译器. ...

  2. HBase学习之路 (一)HBase基础介绍

    产生背景 自 1970 年以来,关系数据库用于数据存储和维护有关问题的解决方案.大数据的出现后, 好多公司实现处理大数据并从中受益,并开始选择像 Hadoop 的解决方案.Hadoop 使用分 布式文 ...

  3. robotframwork接口测试(五)—接口分层测试粗解

    个人小结,仅供参考. 接口测试很简单,但是很重要. 可以写代码,也可以用工具进行测试.工具说说就很多了,简单介绍一下我目前用过的几个能够测试接口的工具, Burpsuite:这类偏请求攻击类软件 Fi ...

  4. new的三种形态

    C++语言一直被认为是复杂编程语言中的杰出代表之一,不仅仅是因为其繁缛的语法规则,还因为其晦涩的术语.下面要讲的就是你的老熟人—new: 它是一个内存管理的操作符,能够从堆中划分一块区域,自动调用构造 ...

  5. PAT乙级1007

    1007 素数对猜想 (20 分) 让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想 ...

  6. oracle什么时候须要commit

    今天在oracle的SQL plus 中运行了删除和查询操作,然后在PL/SQL中也运行查询操作,语句一样,结果却不一样,让我大感郁闷,后来才突然想到可能是两边数据不一致造成的,可是为什么不一致呢,就 ...

  7. centos安装telnet

    安装环境:CentOS 6.4    上篇已经讲述了memcached的安装,现在要测试Memcached功能的时候,需要使用到telnet服务.于是就有了本篇.   一.安装telnet 1.检测t ...

  8. pyhton 面向对象之 小明左右手换牌

    '''#左右手交换牌 案列#小明手里有俩张牌,左手红桃♥K,右手黑桃♠A,小明交换俩手的牌后,手里分别是什么? 人类:    属性:小明,左手,右手    行为:展示手里的牌, 交换手里的牌手类:   ...

  9. vue的采坑之旅--vue-cli脚手架loader重复--Invalid CSS after "...load the styles": expected 1 selector or at-rule

    在使用scss是可能会添加loader,例如 { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'], } 然而当使 ...

  10. SVG 动画(animate、animateTransform、animateMotion)

    原文:https://blog.csdn.net/chy555chy/article/details/53535581 参考 MDN开发文档 https://developer.mozilla.org ...