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. Verilog杂谈

    1. Testbech总是用reg去驱动DUT的input端口,因为需要在仿真期间设置和保持输入端的值(例如在initial中设置初值,在always中设置激励值): 2. 避免对局部reg在定义时赋 ...

  2. linux时间管理

    /etc/sysconfig/clock         该配置文件可用来设置用户选择何种方式显示时间.如果硬件时钟为本地时间,则UTC设为0,并且不用设置环境变量TZ.如果硬件时钟为UTC时间,则要 ...

  3. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  4. Linux下SSH的Log文件路径

    Redhat or Fedora Core: /var/log/secure # Mandrake, FreeBSD or OpenBSD: /var/log/auth.log # SuSE: /va ...

  5. 在Myeclipse中移除项目对Hibernate的支持

    在Myeclipse中移除项目对Hibernate的支持 在使用Hibernate框架进行开发时可能会遇到配置错误或者需要删除Hibernate支持的情况.下面就说一下如何彻底移除项目的Hiberna ...

  6. 【freemaker】之eclipse安装freemaker-IDE

    eclipse安装插件一般我喜欢手动安装,有种可控的感觉! 首先需要下载freemaker-IDEhttp://freemarker-ide.sourceforge.net/ 下载之后解压得到 在ec ...

  7. u-boot启动流程分析(2)_板级(board)部分

    转自:http://www.wowotech.net/u-boot/boot_flow_2.html 目录: 1. 前言 2. Generic Board 3. _main 4. global dat ...

  8. @synthesize obj = _obj 理解

    在很多代码里可以看到类似得用法: @interface MyClass:NSObject{ MyObjecct *_object; } @property(nonamtic, retain) MyOb ...

  9. Segment fault及LINUX core dump详解

    源自:http://andyniu.iteye.com/blog/1965571 core dump的概念: A core dump is the recorded state of the work ...

  10. C和C++混合编程

    extern "C"表示编译生成的内部符号名使用C约定.C++支持函数重载,而C不支持,两者的编译规则也不一样.函数被C++编译后在符号库中的名字与C语言的不 同.例如,假设某个函 ...