cursor --------需要用户先定义,在select时,可以用于处理多行记录

1、declare  声明一个游标

2、open cursor (隐式游标自动open)

3、fetch cursor 读取记录到变量(在select时,可以通过循环的方式读取多行记录)

4、判断游标是否为空(到达最后一行记录)

5、close cusor 关闭游标

%isopen  判断游标是否open %found    判断游标是否为非空 %notfound  判断游标是否为空 %rowcount   在游标中处理的数据行数

①案例:通过显式游标select读取多行数据

SQL> declare
2 cursor cur_emp is
3 select * from emp where deptno=&no;
4
5 emp_rec emp%rowtype;
6
7 begin
8 if not cur_emp%isopen then
9 open cur_emp;
10 end if;
11
12 loop
13 fetch cur_emp into emp_rec ;
14 exit when cur_emp%notfound ;
15
16 dbms_output.put_line( emp_rec.ename ||' , '||emp_rec.sal||' , '|| emp_rec.deptno );
17 end loop;
18 close cur_emp;
19
20 end;

②通过for循环读取游标数据:

SQL> declare
2 cursor cur_emp is
3 select * from emp where deptno=&no;
4
5 begin
6 for emp_rec in cur_emp loop
7 dbms_output.put_line( emp_rec.ename ||' , '||emp_rec.sal||' , '|| emp_rec.deptno );
8 end loop;
9
10 end;

③带有参数的游标:通过参数传递给游标

SQL> declare
2
3 cursor emp_cur (v_deptno number) is
4 select * from emp where deptno=v_deptno;
5 emp_rec emp%rowtype;
6
7 begin
8 if not emp_cur%isopen then
9 open emp_cur(30);
10 end if;
11 loop
12 fetch emp_cur into emp_rec ;
13 exit when emp_cur%notfound;
14 dbms_output.put_line( emp_rec.ename ||' , '||emp_rec.sal||' , '|| emp_rec.deptno );
15 end loop;
16 end;
SQL>  declare
2
3 cursor emp_cur (v_deptno number) is
4 select * from emp where deptno=v_deptno;
5 emp_rec emp%rowtype;
6
7 begin
8 if not emp_cur%isopen then
9 open emp_cur(20);
10 end if;
11 loop
12 fetch emp_cur into emp_rec ;
13 exit when emp_cur%notfound;
14 dbms_output.put_line( emp_rec.ename ||' , '||emp_rec.sal||' , '|| emp_rec.deptno );
15 end loop;
16 end;

④在for循环中嵌套游标(游标不需要declare)

SQL> begin
2 for emp_rec in (select * from emp where deptno=&no) loop
3 dbms_output.put_line( emp_rec.ename ||' , '||emp_rec.sal||' , '|| emp_rec.deptno );
4 end loop;
5
6 end;

【PL/SQL练习】显式游标的更多相关文章

  1. 【Oracle】PL/SQL 显式游标、隐式游标、动态游标

    在PL/SQL块中执行SELECT.INSERT.DELETE和UPDATE语句时,Oracle会在内存中为其分配上下文区(Context Area),即缓冲区.游标是指向该区的一个指针,或是命名一个 ...

  2. PL/SQL — 显式游标

    一.游标的相关概念及特性 1.定义 通过游标方式定位到结果集中某个特定的行,然后根据业务需求对该行进行相应特定的操作. 2.分类 显示游标: 用户自定义游标,用于处理select语句返回的多行数据. ...

  3. plsql 显式游标

    显式游标的处理过程包括: 声明游标,打开游标,检索游标,关闭游标. 声明游标 CURSOR c_cursor_name IS statement; 游标相当于一个查询结果集,将查询的结果放在游标里,方 ...

  4. PL/SQL学习笔记之游标

    一:游标 Oracle会创建一个上下文区域,用于处理SQL语句,其中包含需要处理的语句.处理结果等等. 游标指向这一上下文的区域. PL/SQL通过控制游标在上下文区域移动,来获取SQL语句的结果信息 ...

  5. PL/SQL学习笔记_02_游标

    在 PL/SQL 程序中,对于处理多行记录的事务经常使用游标来实现. 为了处理 SQL 语句, ORACLE 必须分配一片叫上下文( context area )的区域来处理所必需的信息,其中包括要处 ...

  6. ORACLE的显式游标与隐式游标

    1)查询返回单行记录时→隐式游标: 2)查询返回多行记录并逐行进行处理时→显式游标 显式游标例子: DECLARE CURSOR CUR_EMP IS SELECT * FROM EMP; ROW_E ...

  7. SQL Server显式事务与隐式事务

    事务是单个的工作单元.如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的永久组成部分.如果事务遇到错误且必须取消或回滚,则所有数据库修改均被清除. SQL Server中有一下几 ...

  8. Oracle PLSQL Demo - 08.定义显式游标[Define CURSOR, Open, Fetch, Close CURSOR]

    declare v_empno scott.emp.empno%type; v_sal scott.emp.sal%type; cursor cur_emp is select t.empno, t. ...

  9. PL/SQL之--游标

    一.游标简介 在PL/SQL中执行SELECT.INSERT.DELETE和UPDATE语句时,ORACLE会在内存中为其分配上下文区(Context Area),也称为缓冲区.游标是指向该区的一个指 ...

随机推荐

  1. CentOS生产机器禁止ROOT远程SSH登录

    方法一 很多站长拥有linux主机,不管是虚拟机还是实体机,一般我们远程连接的时候,都是用的ssh(SecureShell建立在应用层和传输层基础上的安全协议). 它默认的端口22,默认使用root也 ...

  2. Learning Puppet — Manifests

    Begin In a text editor — vim, emacs, or nano — create a file with the following contents and filenam ...

  3. [备忘]Visio中连接线交叉时跨线小弯的去掉方法

    连接线格式->行为->连接线->跨线->添加->从不 format->behavior…->Connector->Line jumps->Add: ...

  4. 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键

    1.新建一个类 HotkeyHelper  using System; using System.Runtime.InteropServices; using System.Windows.Forms ...

  5. (C#) 多线程修改布尔值, volatile

    参考: https://msdn.microsoft.com/en-us/library/x13ttww7(VS.80).aspx http://stackoverflow.com/questions ...

  6. Shell中set用法(转载)

    使用set命令可以设置各种shell选项或者列出shell变量. 单个选项设置常用的特性. 在某些选项之后-o参数将特殊特性打开. 在某些选项之后使用+o参数将关闭某些特性, 不带任何参数的set命令 ...

  7. 使用matplot绘图 @python

    1. 使用csv 模块读取数据 2. 定义label 3. 绘图,调参 #!/usr/bin/env python # coding=utf-8 import sys import matplotli ...

  8. jQuery 常见操作实现方式

    一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备忘. jQuery 常见操作实现方式 $("标签名") //取html元素 document. ...

  9. sqlite 时间排序

    select * from tb_QuantifyResult where iSamplingOrCalibration = 1 and cComponentName <> ' + Quo ...

  10. protoc的protoc-gen-grpc-java插件

    编译 protoc-gen-grpc-java插件 的文档在: https://github.com/grpc/grpc-java/tree/master/compiler  编译的步骤: Chang ...