一、游标

1. 使用游标

要求: 打印出 80 部门的所有的员工的工资: salary: xxx

declare

--1. 定义游标

cursor salary_cursor is select salary from employees where department_id = 80;

v_salary employees.salary%type;

begin

--2. 打开游标

open salary_cursor;

--3. 提取游标

fetch salary_cursor into v_salary;

--4. 对游标进行循环操作: 判断游标中是否有下一条记录

while salary_cursor%found loop

dbms_output.put_line('salary: ' || v_salary);

fetch salary_cursor into v_salary;

end loop;

--5. 关闭游标

close  salary_cursor;

end;

2. 使用游标的练习: 打印出 manager_id 为 100 的员工的 last_name, email, salary 信息(使用游标, 记录)

declare

--声明游标

cursor emp_cursor is select last_name, email, salary from employees where manager_id = 100;

--声明记录类型

type emp_record is record(

name employees.last_name%type,

email employees.email%type,

salary employees.salary%type

);

-- 声明记录类型的变量

v_emp_recorde emp_record;

begin

--打开游标

open emp_cursor;

--提取游标

fetch emp_cursor into v_emp_recorde;

--对游标进行循环操作

while emp_cursor%found loop

dbms_output.put_line(v_emp_recorde.name || ', ' || v_emp_recorde.email || ', ' || v_emp_recorde.salary );

fetch emp_cursor into v_emp_recorde;

end loop;

--关闭游标

close emp_cursor;

end;

3. 利用游标, 调整公司中员工的工资:

工资范围       调整基数

0 - 5000       5%

5000 - 10000   3%

10000 - 15000  2%

15000 -        1%

declare

--定义游标

cursor emp_sal_cursor is select salary, employee_id from employees;

--定义基数变量

temp number(4, 2);

--定义存放游标值的变量

v_sal employees.salary%type;

v_id employees.employee_id%type;

begin

--打开游标

open emp_sal_cursor;

--提取游标

fetch emp_sal_cursor into v_sal, v_id;

--处理游标的循环操作

while emp_sal_cursor%found loop

--判断员工的工资, 执行 update 操作

--dbms_output.put_line(v_id || ': ' || v_sal);

if v_sal <= 5000 then

temp := 0.05;

elsif v_sal<= 10000 then

temp := 0.03;

elsif v_sal <= 15000 then

temp := 0.02;

else

temp := 0.01;

end if;

--dbms_output.put_line(v_id || ': ' || v_sal || ', ' || temp);

update employees set salary = salary * (1 + temp) where employee_id = v_id;

fetch emp_sal_cursor into v_sal, v_id;

end loop;

--关闭游标

close emp_sal_cursor;

end;

使用 decode 函数

update employees set salary = salary * (1 + (decode(trunc(salary/5000), 0, 0.05,

1, 0.03,

2, 0.02,

0.01)))

4. 利用游标 for 循环完成 3.

declare

--定义游标

cursor emp_sal_cursor is select salary, employee_id id from employees;

--定义基数变量

temp number(4, 2);

begin

--处理游标的循环操作

for c in emp_sal_cursor loop

--判断员工的工资, 执行 update 操作

--dbms_output.put_line(v_id || ': ' || v_sal);

if c.salary <= 5000 then

temp := 0.05;

elsif c.salary <= 10000 then

temp := 0.03;

elsif c.salary <= 15000 then

temp := 0.02;

else

temp := 0.01;

end if;

--dbms_output.put_line(v_id || ': ' || v_sal || ', ' || temp);

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

end loop;

end;

5. 带参数的游标

declare

--定义游标

cursor emp_sal_cursor(dept_id number, sal number) is

select salary + 1000 sal, employee_id id

from employees

where department_id = dept_id and salary > sal;

--定义基数变量

temp number(4, 2);

begin

--处理游标的循环操作

for c in emp_sal_cursor(sal => 10000, dept_id => 80) loop

--判断员工的工资, 执行 update 操作

--dbms_output.put_line(v_id || ': ' || v_sal);

if c.sal <= 5000 then

temp := 0.05;

elsif c.sal <= 10000 then

temp := 0.03;

elsif c.sal <= 15000 then

temp := 0.02;

else

temp := 0.01;

end if;

--dbms_output.put_line(c.sal || ': ' || c.id || ', ' || temp);

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

end loop;

end;

6. 隐式游标: 更新指定员工 salary(涨工资 10),如果该员工没有找到,则打印”查无此人” 信息

begin

update employees set salary = salary + 10 where employee_id = 1005;

if sql%notfound then

dbms_output.put_line('查无此人!');

end if;

end;

二、异常

1. 异常的基本程序: 通过 select ... into ... 查询某人的工资, 若没有查询到, 则输出 "未找到数据"

declare

--定义一个变量

v_sal employees.salary%type;

begin

--使用 select ... into ... 为 v_sal 赋值

select salary into v_sal from employees where employee_id = 1000;

dbms_output.put_line('salary: ' || v_sal);

exception

when No_data_found then

dbms_output.put_line('未找到数据');

end;

declare

--定义一个变量

v_sal employees.salary%type;

begin

--使用 select ... into ... 为 v_sal 赋值

select salary into v_sal from employees;

dbms_output.put_line('salary: ' || v_sal);

exception

when No_data_found then

dbms_output.put_line('未找到数据!');

when Too_many_rows then

dbms_output.put_line('数据过多!');

end;

2. 处理非预定义的异常处理: "违反完整约束条件"

declare

--1. 定义异常

temp_exception exception;

--2. 将其定义好的异常情况,与标准的ORACLE错误联系起来,使用EXCEPTION_INIT语句

PRAGMA EXCEPTION_INIT(temp_exception, -2292);

begin

delete from employees where employee_id = 100;

exception

--3. 处理异常

when temp_exception then

dbms_output.put_line('违反完整性约束!');

end;

3. 自定义异常: 更新指定员工工资,增加100;若该员工不存在则抛出用户自定义异常: no_result

declare

--自定义异常

no_result exception;

begin

update employees set salary = salary + 100 where employee_id = 1001;

--使用隐式游标, 抛出自定义异常

if sql%notfound then

raise no_result;

end if;

exception

--处理程序抛出的异常

when no_result then

dbms_output.put_line('更新失败');

end;

oracle 10g 学习之游标使用和异常介绍(11)的更多相关文章

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

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

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

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

  3. oracle 10g 学习之数据进行增删改查、数据库事务、约束(8)

    目标 通过本章学习,您将可以: l  使用 DML 语句 l  向表中插入数据 l  更新表中数据 l  从表中删除数据 l  控制事务 l  描述约束 l  创建和维护约束 数据控制语言 l     ...

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

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

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

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

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

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

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

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

  8. oracle 10g 学习之基本 SQL SELECT 语句(4)

    本篇文章中,对于有的和MSSQL Server相同的语法我就没有再写了,这里我只写Oracle和MSSQL Server有点不同的 定义空值 l  空值是无效的,未指定的,未知的或不可预知的值 l  ...

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

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

随机推荐

  1. CodeForces 701B Cells Not Under Attack

    题目链接:http://codeforces.com/problemset/problem/701/B 题目大意: 输入一个数n,m, 生成n*n的矩阵,用户输入m个点的位置,该点会影响该行和该列,每 ...

  2. 通过开源程序同时解决DNS劫持和DNS污染的问题

    我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...

  3. 简单聊下IO复用

    没图,不分析API Java中IO API的发展:Socket -> SocketChannel -> AsynchronousSocketChannelServerSocket -> ...

  4. WinAPI【远程注入】三种注入方案【转】

    来源:http://www.cnblogs.com/okwary/archive/2008/12/20/1358788.html 导言: 我 们在Code project(www.codeprojec ...

  5. 教你安装漂亮的Arc GTK主题

    导读 近日,我们又发现了一款深受 Linux 用户喜爱的桌面主题 — Arc GTK,Arc GTK 主题已被很多 GNU/Linux 操作系统支持和采用,其中就包括即将到来的 Linux Mint ...

  6. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  7. MYSQL注入天书之HTTP头部介绍

    Background-5 HTTP头部介绍 在利用抓包工具进行抓包的时候,我们能看到很多的项,下面详细讲解每一项. HTTP头部详解 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* ...

  8. linux下软件安装的方法

    linux下软件的安装与卸载   第一章   linux下安装软件,如何知道软件安装位置 注:一般的软件的默认安装目录在 jdk-1_6_0_14-linux-i586-rpm.bin    ←修改为 ...

  9. JS 自定义正则表达式

    1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...

  10. [BZOJ]1016 JSOI2008 最小生成树计数

    最小生成树计数 题目描述 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同 ...