一、函数

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;

oracle 10g 学习之函数和存储过程(12)的更多相关文章

  1. oracle 10G 没有 PIVOT 函数怎么办,自己写一个不久有了

    众所周知,静态SQL的输出结构必须也是静态的.对于经典的行转列问题,如果行数不定导致输出的列数不定,标准的答案就是使用动态SQL, 到11G里面则有XML结果的PIVOT. 但是 oracle 10G ...

  2. oracle 10g 学习之服务器端安装(1)

    Oracle 简介 lOracle 是殷墟出土的甲骨文(oracle bone inscriptions)的英文翻译的第一个单词 lOracle 公司是全球最大的信息管理软件及服务供应商,成立于197 ...

  3. oracle 10g 学习之单行函数(5)

    目标 通过本章学习,您将可以: l  SQL中不同类型的函数. l  在 SELECT 语句中使用字符,数字和日期函数. l  描述转换型函数的用途. 字符函数 字符函数分为大小写控制函数和字符控制函 ...

  4. oracle 10g 学习之多表查询、分组函数(6)

    笛卡尔集 l  笛卡尔集会在下面条件下产生: 省略连接条件 连接条件无效 所有表中的所有行互相连接 l  为了避免笛卡尔集, 可以在 WHERE 加入有效的连接条件. 自连接 select m.las ...

  5. oracle 10g 学习之PL/SQL简介和简单使用(10)

    PL /SQL是一种高级数据库程序设计语言,该语言专门用于在各种环境下对ORACLE数据库进行访问.由于该语言集成于数据库服务器中,所以PL/SQL代码可以对数据进行快速高效的处理.PL/SQL是 P ...

  6. oracle 10g 学习之创建和管理表(7)

    目标 通过本章学习,您将可以: l  描述主要的数据库对象. l  创建表. l  描述各种数据类型. l  修改表的定义. l  删除,重命名和清空表. 常见的数据库对象 表.视图.序列.索引.同义 ...

  7. oracle 10g 学习之oracle管理(3)

    怎样将预先写好的sql脚本执行? select * from employees;→107条记录 利用 Oracle 企业管理器连接数据库服务器 点击打开以下界面: 此时已经连接成功了 用 Oracl ...

  8. Oracle 11g系列:函数与存储过程

    1.函数 Oracle中的函数分为两类:系统函数和自定义行数.对于自定义函数,函数的传入参数可以没有,如果有,一定要明确其数据类型.函数传入参数不能在函数内部进行修改.函数必须有返回值,并且返回值必须 ...

  9. oracle 10g 学习之.NET使用Oracle数据库(14)

    因为使用System.Data.OracleClient会提示过时,推荐使用oracle自己提供的.net类库Oracle.DataAccess.Client 在oracle C:\oracle\pr ...

随机推荐

  1. yum被锁Another app is currently holding the yum lock; waiting for it to exit...

    可能是系统自动升级正在运行,yum在锁定状态中. 可以通过强制关掉yum进程: #rm -f /var/run/yum.pid 然后就可以使用yum了.

  2. SpringMVC 和Struts2的区别

    SpringMVC 和Struts2的区别 1. 机制: spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能: spring会稍微比s ...

  3. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

  4. POJ 1836 Alignment

    Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11450 Accepted: 3647 Descriptio ...

  5. 用LR12录制app,用LR11跑场景,无并发数限制,已试验过,可行!

    免费使用LoadRunner对移动互联网后端服务器压力测试 一.LoadRunner简介 LoadRunner,是惠普公司研发的一款预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及 ...

  6. mongo数据库的导入导出

    http://www.iwangzheng.com/ [root@a02]$show dbs; changhong_tv_cms 0.078GB [root@a02]$ mongodump -d ch ...

  7. session 实现登录功能注意事项

    php5之后废除了session_unregister()函数,可以用 session_destory().其他的也都没有啥了,还有就是输出嵌入的PHP代码用=代码见我的git:https://git ...

  8. MotionEvent常见值

    常见的动作常量: public static final int ACTION_DOWN        = 0;单点触摸动作 public static final int ACTION_UP     ...

  9. hdu4255筛素数+广搜

    Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...

  10. javascript return false 详解

    在大多数情况下,为事件处理函数返回false,可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转到该元素href属性指定的页. Return False 就相当于终止符 ...