一. 使用for循环游标:遍历全部职位为经理的雇员

1. 定义游标(游标就是一个小集合)

2. 定义游标变量

3. 使用for循环游标

declare
-- 定义游标c_job
cursor c_job is
select empno, ename, job, sal from emp where job = 'MANAGER'; -- 定义游标变量c_row
c_row c_job%rowtype;
begin
-- 循环游标,用游标变量c_row存循环出的值
for c_row in c_job loop
dbms_output.put_line(c_row.empno || '-' || c_row.ename || '-' ||
c_row.job || '-' || c_row.sal);
end loop;
end;

二. fetch游标:遍历全部职位为经理的雇员

使用的时候必须明白的打开和关闭

declare
--定义游标c_job
cursor c_job is
select empno, ename, job, sal from emp where job = 'MANAGER'; --定义游标变量c_row
c_row c_job%rowtype;
begin
open c_job;
loop
--提取一行数据到c_row
fetch c_job into c_row; --判读是否提取到值。没取到值就退出
exit when c_job%notfound;
dbms_output.put_line(c_row.empno || '-' || c_row.ename || '-' ||
c_row.job || '-' || c_row.sal);
end loop; --关闭游标
close c_job;
end;

三. 使用游标和while循环:遍历全部部门的地理位置

--3,使用游标和while循环来显示全部部门的的地理位置(用%found属性)
declare
--声明游标
cursor csr_TestWhile is select loc from dept; --指定行指针
row_loc csr_TestWhile%rowtype;
begin
open csr_TestWhile;
--给第一行数据
fetch csr_TestWhile into row_loc; --測试是否有数据,并运行循环
while csr_TestWhile%found loop
dbms_output.put_line('部门地点:' || row_loc.LOC);
--给下一行数据
fetch csr_TestWhile into row_loc;
end loop;
close csr_TestWhile;
end;

四. 带參的游标:接受用户输入的部门编号

declare
-- 带參的游标
cursor c_dept(p_deptNo number) is
select * from emp where emp.deptno = p_deptNo; r_emp emp%rowtype;
begin
for r_emp in c_dept(20) loop
dbms_output.put_line('员工号:' || r_emp.EMPNO || '员工名:'
|| r_emp.ENAME || '工资:' || r_emp.SAL);
end loop;
end;

五. 加锁的游标:对全部的salesman添加佣金500

declare
--查询数据,加锁(for update of)
cursor csr_addComm(p_job nvarchar2) is
select * from emp where job = p_job for update of comm;
r_addComm emp%rowtype;
commInfo emp.comm%type;
begin
for r_addComm in csr_addComm('SALESMAN') loop
commInfo := r_addComm.comm + 500; --更新数据(where current of)
update emp set comm = commInfo where current of csr_addComm;
end loop;
end;

六. 使用计数器:找出两个工作时间最长的员工

declare
cursor crs_testComput is
select * from emp order by hiredate asc; --计数器
top_two number := 2;
r_testComput crs_testComput%rowtype;
begin
open crs_testComput;
fetch crs_testComput into r_testComput;
while top_two > 0 loop
dbms_output.put_line('员工姓名:' || r_testComput.ename ||
' 工作时间:' || r_testComput.hiredate);
--计速器减1
top_two := top_two - 1;
fetch crs_testComput into r_testComput;
end loop;
close crs_testComput;
end;

七. if/else推断:对全部员工按基本薪水的20%加薪。假设添加的薪水大于300就取消加薪

declare
cursor crs_upadateSal is
select * from emp for update of sal;
r_updateSal crs_upadateSal%rowtype;
salAdd emp.sal%type;
salInfo emp.sal%type;
begin
for r_updateSal in crs_upadateSal loop
salAdd := r_updateSal.sal * 0.2;
if salAdd > 300 then
salInfo := r_updateSal.sal;
dbms_output.put_line(r_updateSal.ename || ': 加薪失败。' ||
'薪水维持在:' || r_updateSal.sal);
else
salInfo := r_updateSal.sal + salAdd;
dbms_output.put_line(r_updateSal.ENAME || ': 加薪成功.' ||
'薪水变为:' || salInfo);
end if;
update emp set sal = salInfo where current of crs_upadateSal;
end loop;
end;

八. 使用case
when:按部门进行加薪

declare
cursor crs_caseTest is
select * from emp for update of sal; r_caseTest crs_caseTest%rowtype;
salInfo emp.sal%type;
begin
for r_caseTest in crs_caseTest loop
case
when r_caseTest.deptno = 10 THEN
salInfo := r_caseTest.sal * 1.05;
when r_caseTest.deptno = 20 THEN
salInfo := r_caseTest.sal * 1.1;
when r_caseTest.deptno = 30 THEN
salInfo := r_caseTest.sal * 1.15;
when r_caseTest.deptno = 40 THEN
salInfo := r_caseTest.sal * 1.2;
end case;
update emp set sal = salInfo where current of crs_caseTest;
end loop;
end;

九. 异常处理:数据回滚

set serveroutput on;
declare
d_name varchar2(20);
begin
d_name := 'developer'; savepoint A;
insert into DEPT values (50, d_name, 'beijing');
savepoint B;
insert into DEPT values (40, d_name, 'shanghai');
savepoint C; exception when others then
dbms_output.put_line('error happens');
rollback to A;
commit;
end;
/

十. 基本指令:

set serveroutput on size 1000000 format wrapped; --使DBMS_OUTPUT有效,并设置成最大buffer,防止"吃掉"最前面的空格
set linesize 256; --设置一行能够容纳的字符数
set pagesize 50; --设置一页有多少行数
set arraysize 5000; --设置来回数据显示量,这个值会影响autotrace时一致性读等数据
set newpage none; --页和页之间不设不论什么间隔
set long 5000; --LONG或CLOB显示的长度
set trimspool on; --将SPOOL输出中每行后面多余的空格去掉
set timing on; --设置查询耗时
col plan_plus_exp format a120; --autotrace后explain plan output的格式
set termout off; --在屏幕上暂不显示输出的内容,为以下的设置sql做准备
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; --设置时间格式

小知识:

以下的语句一定要在Command Window里面才干打印出内容

set serveroutput on;
begin
dbms_output.put_line('hello!');
end;
/

oracle存储过程的使用的更多相关文章

  1. oracle 存储过程

    来自:http://www.jb51.net/article/31805.htm Oracle存储过程基本语法 存储过程 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 I ...

  2. Oracle存储过程语法

    原文链接:http://www.jb51.net/article/31805.htm Oracle存储过程基本语法 存储过程  1 CREATE OR REPLACE PROCEDURE 存储过程名  ...

  3. ORACLE存储过程调用Web Service

    1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...

  4. Oracle存储过程基本语法介绍

    Oracle存储过程基本语法 存储过程 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 IS 3 BEGIN 4 NULL; 5 END; 行1: CREATE OR RE ...

  5. MyBatis调用Oracle存储过程

    MyBatis调用Oracle存储过程 1.无输入和输出参数的存储过程 2.带有输入和输出参数的存储过程 3.返回游标的存储过程 mybatis中的配置文件代码 <resultMap type= ...

  6. Oracle存储过程(转)

    Oracle存储过程基本语法 存储过程 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 IS 3 BEGIN 4 NULL; 5 END; 行1: CREATE OR RE ...

  7. Oracle存储过程中异常Exception的捕捉和处理

    Oracle存储过程中异常的捕捉和处理 CREATE OR REPLACE Procedure Proc_error_process ( v_IN in Varchar2, v_OUT Out Var ...

  8. Oracle存储过程动态创建临时表/存储过程执行权限问题--AUTHID CURRENT_USER

    关于Oracle存储过程执行权限问题的解决 http://blog.sina.com.cn/s/blog_6ceed3280101hvlo.html (2014-04-02 04:06:28) 转载▼ ...

  9. ORACLE存储过程学习

    存储过程 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 IS 3 BEGIN 4 NULL; 5 END; 行1: CREATE OR REPLACE PROCEDURE ...

  10. C# 获取 oracle 存储过程的 返回值1

    /// <summary> /// 返回对应表的模拟自增字段值 /// </summary> /// <param name="tablename"& ...

随机推荐

  1. [ligerUI] grid封装调用方法

    /** * 获取页面参数 */ function getPageSize(){ var xScroll, yScroll; if (window.innerHeight && wind ...

  2. hihoCoder-1633 ACM-ICPC北京赛区2017 G.Liaoning Ship’s Voyage 线段与三角形规范相交

    题面 题意:给你一个20*20的地图,起点(0,0),终点(n-1,n-1),有障碍的点为‘#’,每次可以向8个方向走一步,还给了一个三角形,除了障碍以外,到这8个方向上的点的线段如果没有与三角形相交 ...

  3. PLSQL简介

    目录 什么是PLSQL PLSQL起源 PLSQL早期版本 改善可移植性 改进执行权限于事务的完整性 原书:steven feuerstei-oracle PLSQL grogramming 2014 ...

  4. layoutInflater的用途以及获取VIEW方法

    如果需要用到自定义多个布局,就需要用到layoutInflater,获取layoutInflater一般有几种方式,但我在实际使用中,感觉如下的getLayoutInflater()是最为方便的,不用 ...

  5. 【Oracle】利用trace文件重建控制文件

    重建步骤: 1.生成trace文件: alter database backup controlfile to trace; 2.找到生成的trace文件: show parameter dump u ...

  6. 【Five-Minute Share】“请先了解所使用的工具 ,磨刀不误砍柴工”

    数据是应用系统的血液,没有数据的系统应用价值是非常有限的.经过多年的观察发现,身边很多的程序开发人员在开发应用系统的时候,都是按照标准SQL语法及应用方法去进行数据库设计,并进行应用开发的,没有任何的 ...

  7. Swift 字典 Dictionary基本用法

    import UIKit /* 字典的介绍 1.字典允许按照某个键访问元素 2.字典是由两部分组成, 一个键(key)集合, 一个是值(value)集合 3.键集合是不能有重复的元素, 值集合可以有重 ...

  8. 【领略RxSwift源码】- 变换操作(Operators)

    在上一篇中,我们分析了在RxSwift中的整个订阅流程.在开讲变换操作之前,首先要弄清楚Sink的概念,不清楚的同学可以翻看上一篇的分析.简单的来说,在每一次订阅操作之前都会进行一次Sink对流的操作 ...

  9. Javaee 方法的构建和调用

    主类: package Szy02; public class Person { String name; int age; String sex; public Person(){ } public ...

  10. 洛谷P1563 玩具谜题 简单模拟

    没意义,注意方向别判错. Code: #include<cstdio> #include<cstring> using namespace std; const int max ...