oracle 10g 学习之函数和存储过程(12)
一、函数
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)的更多相关文章
- oracle 10G 没有 PIVOT 函数怎么办,自己写一个不久有了
众所周知,静态SQL的输出结构必须也是静态的.对于经典的行转列问题,如果行数不定导致输出的列数不定,标准的答案就是使用动态SQL, 到11G里面则有XML结果的PIVOT. 但是 oracle 10G ...
- oracle 10g 学习之服务器端安装(1)
Oracle 简介 lOracle 是殷墟出土的甲骨文(oracle bone inscriptions)的英文翻译的第一个单词 lOracle 公司是全球最大的信息管理软件及服务供应商,成立于197 ...
- oracle 10g 学习之单行函数(5)
目标 通过本章学习,您将可以: l SQL中不同类型的函数. l 在 SELECT 语句中使用字符,数字和日期函数. l 描述转换型函数的用途. 字符函数 字符函数分为大小写控制函数和字符控制函 ...
- oracle 10g 学习之多表查询、分组函数(6)
笛卡尔集 l 笛卡尔集会在下面条件下产生: 省略连接条件 连接条件无效 所有表中的所有行互相连接 l 为了避免笛卡尔集, 可以在 WHERE 加入有效的连接条件. 自连接 select m.las ...
- oracle 10g 学习之PL/SQL简介和简单使用(10)
PL /SQL是一种高级数据库程序设计语言,该语言专门用于在各种环境下对ORACLE数据库进行访问.由于该语言集成于数据库服务器中,所以PL/SQL代码可以对数据进行快速高效的处理.PL/SQL是 P ...
- oracle 10g 学习之创建和管理表(7)
目标 通过本章学习,您将可以: l 描述主要的数据库对象. l 创建表. l 描述各种数据类型. l 修改表的定义. l 删除,重命名和清空表. 常见的数据库对象 表.视图.序列.索引.同义 ...
- oracle 10g 学习之oracle管理(3)
怎样将预先写好的sql脚本执行? select * from employees;→107条记录 利用 Oracle 企业管理器连接数据库服务器 点击打开以下界面: 此时已经连接成功了 用 Oracl ...
- Oracle 11g系列:函数与存储过程
1.函数 Oracle中的函数分为两类:系统函数和自定义行数.对于自定义函数,函数的传入参数可以没有,如果有,一定要明确其数据类型.函数传入参数不能在函数内部进行修改.函数必须有返回值,并且返回值必须 ...
- oracle 10g 学习之.NET使用Oracle数据库(14)
因为使用System.Data.OracleClient会提示过时,推荐使用oracle自己提供的.net类库Oracle.DataAccess.Client 在oracle C:\oracle\pr ...
随机推荐
- C# Attribute 特性 学习
一.特性的概述 公共语言运行库允许您添加类似关键字的描述性声明(称为特性 (Attribute))来批注编程元素,如类型.字段.方法和属性 (Property).属性与 Microsoft .NET ...
- 关于talbeViewCell一点感想
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
- linux (centos) 单机50w+链接 内核参数配置
1 突破系统最大fd 查看当前文件描述符的限制数目的命令: ulimit -n .修改文件描述符的限制数目 2.1 临时改变当前会话: ulimit -n 2.2 永久变更需要下面两个步骤: ./ ...
- html5摇一摇[转]
写在前面 年底了,有些公司会出一个摇奖的活动,今天在家没事就搜了一下这方面的资料. 原文地址:http://www.cnblogs.com/waitingbar/p/4682215.html 测试 效 ...
- thinkphp 3.23 第三方登录sdk集成包
本集成包在官方包上扩展了支付宝登录和微信,支持最新的3.23版本 config /* URL配置 */ 'URL_CASE_INSENSITIVE' => true, //默认fa ...
- PhpStorm 设置php代码格式
phpstorm 代码格式化方法: 快捷键:Ctrl + Alt + L 设置代码样式:File -> Settings -> Code Style ->PHP 根据个人php代码规 ...
- Servlet的生命周期及filter,servletRequest和servletResponse
序,Web应用中,Servlet和Filter是很重要的两个概念,一定要理解透彻. 一.Servlet类 继承自HttpServlet,HttpServlet是一个抽象类,主要包含的方法有init,s ...
- HDU 1018 Big Number (数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x ...
- vim在选中的几行中查找并替换
(文章是从我的个人主页上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com) 使用vim编辑器是能带来很大的便捷的,如何替换选中的多行中的某个单词呢. 先选中,按下冒号,左下方 ...
- Android中多个调用Activity的问题
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" ...