一、函数

1. 函数的 helloworld: 返回一个 "helloworld--!" 的字符串

create or replace function helloworld

return varchar2

is

begin

return 'helloworld--!';

end;

执行函数

begin

dbms_output.put_line(helloworld());

end;

2. 定义带参数的函数: 两个数相加

create or replace function add_func(a number, b number)

return number

is

begin

return (a + b);

end;

执行函数

begin

dbms_output.put_line(add_func(12, 13));

end;

3. 定义一个函数: 获取给定部门的工资总和, 要求, 部门号定义为参数, 工资总额定义为返回值.

create or replace function sum_sal(dept_id number)

return number

is

cursor sal_cursor is select salary from employees where department_id = dept_id;

v_sum_sal number(8) := 0;

begin

for c in sal_cursor loop

v_sum_sal := v_sum_sal + c.salary;

end loop;

dbms_output.put_line('sum salary: ' || v_sum_sal);

return v_sum_sal;

end;

执行函数

begin

dbms_output.put_line(sum_sal(80));

end;

4. 关于 OUT 型的参数: 因为函数只能有一个返回值, PL/SQL 程序可以通过 OUT 型的参数实现有多个返回值

要求: 定义一个函数: 获取给定部门的工资总和 和 该部门的员工总数, 要求: 部门号定义为参数, 工资总额定义为返回值.

create or replace function sum_sal(dept_id number, total_count out number)

return number

is

cursor sal_cursor is select salary from employees where department_id = dept_id;

v_sum_sal number(8) := 0;

begin

total_count := 0;

for c in sal_cursor loop

v_sum_sal := v_sum_sal + c.salary;

total_count := total_count + 1;

end loop;

--dbms_output.put_line('sum salary: ' || v_sum_sal);

return v_sum_sal;

end;

执行函数:

delare

v_total number(3) := 0;

begin

dbms_output.put_line(sum_sal(80, v_total));

dbms_output.put_line(v_total);

end;

二、存储过程

基本上和函数一样

1. 定义一个存储过程: 获取给定部门的工资总和(通过 out 参数), 要求, 部门号和工资总额定义为参数

create or replace procedure sum_sal_procedure(dept_id number, v_sum_sal out number)

is

cursor sal_cursor is select salary from employees where department_id = dept_id;

begin

v_sum_sal := 0;

for c in sal_cursor loop

--dbms_output.put_line(c.salary);

v_sum_sal := v_sum_sal + c.salary;

end loop;

dbms_output.put_line('sum salary: ' || v_sum_sal);

end;

2. 自定义一个存储过程完成以下操作:

对给定部门(作为输入参数)的员工进行加薪操作, 若其到公司的时间在 ?  -- 95 期间, 为其加薪 %5 95 – 98 %3  98 --  ?  %1

得到以下返回结果: 为此次加薪公司每月需要额外付出多少成本(定义一个 OUT 型的输出参数).

create or replace procedure add_sal_procedure(dept_id number, temp out number)

is

cursor sal_cursor is select employee_id id, hire_date hd, salary sal from employees where department_id = dept_id;

a number(4, 2) := 0;

begin

temp := 0;

for c in sal_cursor loop

a := 0;

if c.hd < to_date('1995-1-1', 'yyyy-mm-dd') then

a := 0.05;

elsif c.hd < to_date('1998-1-1', 'yyyy-mm-dd') then

a := 0.03;

else

a := 0.01;

end if;

temp := temp + c.sal * a;

update employees set salary = salary * (1 + a) where employee_id = c.id;

end loop;

end;

转载于:https://www.cnblogs.com/yxlblogs/p/3470003.html

oracle 10g 学习之函数和存储过程(12)相关推荐

  1. Oracle 10g学习笔记(一)

    前言 很不好意思的坦白本人是刚刚学习oracle的菜鸟,以下内容是节取的书中一些值得注意的事项,因为只涉及oracle最初级的知识,并没有太深的内容,希望各位oralce高手们能够帮忙指点指点. Or ...

  2. oracle 10g学习之分组函数

    一. (1)分组查询语句的顺序 select ... from ... where ... group by ... having ... order by ... 注意:     where--&g ...

  3. JAVA_WEB Oracle 10g学习: PL/SQL与Oracle间交互

    一.抽象的角色 抽象是对一个真实世界实体的高级描述或建模.它能排除掉无关的细节内容,使我们的日常生活更有条理.例如,驾驶一辆汽车时,我们是不需要知道它的发动机是如何工作的.由变速排档.方向盘.加速器和 ...

  4. goldengate for oracle 10g学习

    选用的是 a reporting configuration with a data pump on the source system + Loading data with a GoldenGat ...

  5. oracle 约束 Oracle 10g学习系列(5)

    这篇文章来自网络,写的挺好,转过来了.  维护数据的完整性 概述:数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则.在Oracle中,数据完整性可以使用约束.触发器.应用程序(过程.函数)三种方 ...

  6. Oracle视图、自定义函数、存储过程、触发器

    视图是一种虚拟表,是实际表的一种映射,主要针对一些复杂查询的一个封装,实际表数据修改,视图数据自动更改 CREATE OR REPLACE VIEW V_TONY AS SELECT A.EMPNO ...

  7. oracle 10g 学习之视图、序列、索引、同义词(9)

    目标 通过本章学习,您将可以: l  描述视图 l  创建和修改视图的定义,删除视图 l  从视图中查询数据 l  通过视图插入, 修改和删除数据 l  使用"Top-N" 分析 ...

  8. oracle wm_concat 替换函数,Oracle 10g无法使用listagg函数的替代解决方案[wm_concat]

    LISTAGG函数介绍 LISTAGG函数是Oracle 11.2新增的函数,用于字符串拼接,11.2之前的版本无法使用,先来简单介绍一下listagg函数的使用 LISTAGG完整语法 以上是官方文 ...

  9. oracle10g无监听配置文件,Oracle 10g学习笔记(七) tnsnames.ora 监听配置文件详解

    监听配置文件 为了使得外部进程 如 CAMS后台程序 能够访问 Oracle 数据库 必须配 置 Oracle 网络服务器环境 配置 Oracle 网络服务器环境是通过配置 listener.ora ...

最新文章

  1. 在CentOS上搭建PHP服务器环境
  2. courses to choose
  3. Java 线程的生命周期
  4. 翻译:SQL Server中的索引内部结构:到SQL Server索引级别10的阶梯。
  5. ASP.NET2.0中配置文件的加密与解密
  6. 二叉树的创建和遍历-C语言实现
  7. linux python whl md5,Python计算一个目录下的所有文件的md5值,在Linux系统下面
  8. java double==Double_java之double类型数值的比较
  9. 利用Kubernetes名称空间来管理内存和CPU资源(一)
  10. QTP自动化测试进阶
  11. iphone6出信号 无服务器,iPhone6手机无服务(非常见故障点)维修
  12. 自动驾驶算法详解(6):Astar算法原理以及路径规划应用在python与ros平台实现
  13. linux系统ss命令详解,ss命令 - Linux命令大全 | linux教程
  14. ssm基于Vue的共享单车app系统
  15. gentos 执行sh文件_学习kaldi跑thchs30记录(run.sh代码过程)
  16. 图片LSB隐写(java)
  17. 【读图】揭密大卖家们的钻展制作流程
  18. CEA-861-D infoframe
  19. stm32呼吸灯实验
  20. 服务应用执行可疑命令

热门文章

  1. c++中vecto容器r常使用的相关函数
  2. 【Tensorflow】Tensor的比较运算
  3. Jsoup解析html某片段的问题
  4. java x%y_java中的运算符
  5. 计算机视觉基础——图像处理(彩色空间互转)cpp+python
  6. Oracle管理权限和角色
  7. C++学习 —— 灵活的继承特性【C++不是玩具,而是生产工具】
  8. win10切换桌面_总说win7最好用,那是你还不知道win10系统,竟有这些强大的功能!...
  9. java 对象初始化过程_Java——对象初始化顺序使用详解
  10. ssh不能进入save方法_快速理清为什么你的redis未授权不能getshell