一、函数

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. 文件流StreamReader和StreamWriter的使用

    using (StreamReader sr = new StreamReader(@"C:\Users\shuai\Desktop\文件流读取.txt", Encoding.De ...

  2. 字符串匹配的KMP算法详解及C#实现

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  3. servlet运行流程

    servlet运行流程  (2013-06-19 19:16:43) 转载▼     首先Servlet被部署到Web容器中,当客户端发送调用这个Servlet的请求到达Web容器时,Web容器会先判 ...

  4. ALTER 语句修改数据表

    1.修改数据表名:alter table 表名 rename 新表名; 2.修改列名: alter table 表名 change 列名 新列名(可以与旧的一样) 类型 默认值; 3.修改类型: al ...

  5. object-c 的ARC 问答/介绍

    原文:http://blog.csdn.net/kmyhy/article/details/8895606 概念" Clangstatic analyzer "是一个非常有用的查找 ...

  6. 容易被忽略的CSS特性

    CSS初学感觉很简单,但随着学习的深入才感觉CSS的水由多深,平常总会遇到各种坑,先总结一些经常遇到的坑 大小写不敏感 虽然我们平时在写CSS的时候都是用小写,但其实CSS并不是大小写敏感的 .tes ...

  7. javascript 对象(DOM)document window history

    Javascript对象 目录: window对象 document对象 history对象 navigator对象 window对象 所有浏览器都支持window对象,它表示浏览器窗口. 所有jav ...

  8. 在表单(input)中id和name的区别

    但是name在以下用途是不能替代的: 1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是 ...

  9. editplus快捷键大全其他editplus快捷键

    editplus快捷键大全其他editplus快捷键,更多快捷键请参考以下文章:editplus快捷键大全之editplus搜索快捷键 editplus快捷键大全之editplus编辑快捷键 edit ...

  10. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...