--创建一个函数,用来根据部门编号返回调薪幅度
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. MySQL主从.md

    MySQL Replication 概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它 ...

  2. Guava包学习--Hash

    我们HashMap会有一个rehash的过程,为什么呢?因为java内建的散列码被限制为32位,而且没有分离散列算法和所作用的数据,所以替代算法比较难做.我们使用HashMap的时候它自身有一个reh ...

  3. 3942: [Usaco2015 Feb]Censoring

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...

  4. Centos7安装anaconda3

    Centos7安装anaconda3 1. 安装bunzip2 yum install bunzip2 2. 下载anaconda3 wget https://repo.anaconda.com/ar ...

  5. c++ 浅拷贝和深拷贝 指针和引用的区别 malloc(free)和new(delete)的区别 重载重写重定义

    4.malloc(free)和new(delete)的区别 malloc()函数: 1.1 malloc的全称是memory allocation,中文叫动态内存分配. 原型:extern void ...

  6. 机器学习中的特征缩放(feature scaling)

    参考:https://blog.csdn.net/iterate7/article/details/78881562 在运用一些机器学习算法的时候不可避免地要对数据进行特征缩放(feature sca ...

  7. 记录一个python公式罗列的方法 join()方法和map()方法的妙用

    题干: 怎样将一个列表中的元素读出,并列出计算式子 比如:[,,,] 输出:+++ = 列表中的元素个数不定 小白和大神的方法: #小白的 numlist=[,,,] sum1='' cal='+' ...

  8. 743. Network Delay Time

    题目来源: https://leetcode.com/problems/network-delay-time/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: class Solution: ...

  9. JavaScript小例子

    1. alert.html <html> <head> <title></title> <script type="text/javas ...

  10. 原生JavaScript技巧大收集

    原生JavaScript技巧大收集 地址:http://itindex.net/detail/47244-javascript