`

oracle游标

阅读更多
--fetch ... into
declare
cursor emp_cursor is select ename,sal from emp where deptno=10;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin

open emp_cursor;
loop
fetch emp_cursor into v_ename,v_sal;
exit when emp_cursor%notfound;
dbms_output.put_line(v_ename|| ' : '||v_sal);
end loop;
    close emp_cursor;
end;

--fetch ... bulk collect into
declare
cursor emp_cursor is
select ename from emp where deptno=10;
type ename_table_type is table of varchar2(10);
ename_table ename_table_type;
begin
open emp_cursor;
fetch emp_cursor bulk collect into ename_table ;
for i in 1.. ename_table.count loop
dbms_output.put_line(ename_table(i));
end loop;
close emp_cursor;
end;

--fetch ... bulk collect into...limit
declare
type name_array_type is varray(5) of varchar2(10);
name_array name_array_type ;
cursor emp_cursor is select ename from emp;
rows int:=5;
v_count int:=0;
begin
open emp_cursor;
loop
fetch emp_cursor bulk collect into name_array limit rows;
dbms_output.put('雇员名:');
for i in 1..(emp_cursor%rowcount-v_count) loop
dbms_output.put(name_array(i)||' ');

end loop;
dbms_output.new_line;
v_count:=emp_cursor%rowcount;
exit when emp_cursor%notfound;
end loop;
close emp_cursor;
end;

--使用游标属性 %isopen %rowcount
declare
cursor emp_cursor is select ename from emp where deptno=10;
type ename_table_type is table of varchar2(20);
ename_table ename_table_type  ;
begin

  if not emp_cursor%isopen then
open emp_cursor;
  end if;
  fetch emp_cursor bulk collect into ename_table;
  for i in 1.. ename_table.count loop
dbms_output.put_line(ename_table(i));
  end loop;
  dbms_output.put_line('提取的总计行数:'||emp_cursor%rowcount);
  close emp_cursor;
end;

--基于游标 定义记录变量
declare
cursor emp_cursor is select ename,sal from emp;
emp_record emp_cursor%rowtype;
begin
open emp_cursor;
loop
   fetch emp_cursor into emp_record;
   dbms_output.put_line('name:'||emp_record.ename||' sal:'||emp_record.sal);
   exit when emp_cursor%notfound ;

end loop;
close emp_cursor;

end;

--带参数游标
declare
cursor emp_cursor(no number) is select ename from emp where deptno=no;
v_ename emp.ename%type;
begin
  open emp_cursor(10);
  loop
    fetch emp_cursor into v_ename;
    dbms_output.put_line(v_ename);
    exit when emp_cursor%notfound;
  end loop;
end;

/*
游标应用
*/
--1使用游标更新数据
declare
cursor emp_cursor is select ename,sal from emp for update;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
   open emp_cursor;
   loop
fetch emp_cursor into v_ename,v_sal;
exit when emp_cursor%notfound;
if v_sal<2000 then
  update emp set sal=sal+200 where current of emp_cursor;
      end if;
   end loop;
   close emp_cursor;
  
end;

--2使用游标删除数据

declare
cursor emp_cursor is select ename,sal,deptno from emp for update;
v_ename emp.ename%type;
v_oldsal emp.sal%type;
v_deptno emp.deptno%type;
begin
open emp_cursor;
loop
fetch emp_cursor into v_ename ,v_oldsal,v_deptno;
exit when emp_cursor%notfound;
if v_deptno=30 then
delete from emp where current of emp_cursor;
end if;
end loop;
close emp_cursor;
end;

---使用Of子句在特定表上加共享锁

declare
cursor emp_cursor is select ename,sal,dname,emp.deptno from emp,dept
where emp.deptno=dept.deptno for update of emp.deptno;
emp_record emp_cursor%rowtype;
begin
open emp_cursor;
loop
fetch emp_cursor into emp_record;
exit when emp_cursor%notfound;
if emp_record.deptno=30 then
update emp set sal=sal+100 where current of emp_cursor;
end if;
dbms_output.put_line('雇员名:'||emp_record.ename||' 工资:'||emp_record.sal||' 部门:'||emp_record.dname);
end loop;
end;

--使用游标for 循环
declare
cursor emp_cursor is select ename ,sal from emp ;
begin
  for emp_record in emp_cursor loop
    dbms_output.put_line('雇员名:'||emp_record.ename||' 薪资:'||emp_record.sal);
  end loop;
end;

--for 循环中直接使用子查询
begin
for emp_record in(
  select ename,sal from emp ) loop
dbms_output.put_line('雇员名:'||emp_record.ename||' 薪资:'||emp_record.sal);
  end loop;
end;
0
0
分享到:
评论

相关推荐

    oracle游标的总结oracle游标的总结

    Oracle 游标概述 Oracle 游标是 Oracle 数据库中的一种重要概念,用于查询数据库,获取记录集合(结果集)的指针。游标可以看作是一个临时表,你可以对其每一行的数据进行任意的操作。本文将对 Oracle 游标的概念、...

    ORACLE 游标使用示例

    下面,我们将深入探讨Oracle游标的使用示例及其相关的知识点。 首先,游标的基本概念是它提供了一种方式来跟踪并控制SQL查询的结果集。在Oracle中,游标有四种状态:未打开、已打开、正在提取和已关闭。以下是一个...

    Oracle游标使用大全

    ### Oracle游标使用详解 #### 一、Oracle游标简介 在Oracle数据库中,游标是一种重要的机制,用于处理查询结果集。它允许用户通过PL/SQL编程语言逐行访问和处理查询返回的数据记录。游标可以是显式定义的(即在...

    Oracle游标使用案例大全

    Oracle游标是数据库编程中非常重要的一个概念,主要用于处理SQL查询的结果集。游标允许我们按行处理数据,逐条读取结果集,而不仅仅是一次性获取所有数据。在Oracle数据库中,游标对于复杂的事务处理、动态SQL以及...

    oracle游标使用及实例

    ### Oracle游标使用及实例详解 #### 一、Oracle游标概述 在Oracle数据库中,游标(Cursor)是一种用于处理SQL查询结果集的方式。它允许用户逐行地读取和处理查询结果,这对于需要对每一行数据进行特定操作的情况非常...

    Oracle游标使用方法及语法大全.doc

    ### Oracle游标使用详解 #### 一、游标概述 游标是Oracle数据库中用于处理查询结果集的强大工具,尤其适用于需要逐行处理查询结果的情况。在Oracle中,游标可以分为两类:**显式游标**和**隐式游标**。 1. **隐式...

    oracle游标学习资料

    Oracle游标是数据库编程中非常重要的一个概念,它允许开发者逐行处理查询结果集,而不仅仅是一次性处理所有数据。在Oracle中,游标分为隐式游标和显式游标。 **一、游标简介** 游标的核心功能是提供一种方式来遍历...

    Oracle游标使用详解

    根据提供的标题、描述以及部分代码内容,我们可以详细探讨Oracle游标的使用方法,特别是明确游标(Explicit Cursor)和隐式游标(Implicit Cursor)的区别及其具体应用方式。 ### Oracle游标简介 在Oracle数据库中...

    Oracle 游标使用大全.pdf

    通过本篇Oracle游标的使用大全,我们可以了解到Oracle数据库游标的类型、属性以及如何在PL/SQL中实现对数据集的逐行处理。这不仅有助于提升程序员的编程技能,也能使他们更深入地理解PL/SQL与Oracle数据库之间的交互...

    Oracle游标使用方法及语法大全

    Oracle 游标使用方法及语法大全 Oracle 游标是 PL/SQL 程序中的一种重要组件,用于处理查询结果集。游标可以分为隐式游标和显式游标两种,隐式游标由 PL/SQL 管理,隐式游标打开时查询开始,查询结束时隐式游标自动...

    oracle游标使用大全1.txt

    ### Oracle游标使用详解 #### 一、Oracle游标简介 在Oracle数据库中,游标是一种用于处理查询结果集的强大工具。它允许用户通过逐行访问数据来执行复杂的操作,如更新、删除或插入记录等。游标可以分为显式游标和...

    oracle 游标 深入浅出 详解 精析 示例

    Oracle游标是数据库管理系统中的一种重要机制,它允许程序员逐行处理查询结果集,而不仅仅是一次性获取所有数据。游标类似于C语言中的指针,能够灵活、高效地处理多条记录,尤其在需要循环处理或者根据当前行数据做...

    快速练习ORACLE游标习题及答案

    根据提供的文件信息,我们可以归纳出以下Oracle游标的使用方法及相关知识点: ### 一、游标的基本概念 在Oracle数据库中,游标是一种重要的机制,它允许用户从查询结果集中逐行检索数据。游标可以分为两种类型:**...

    ORACLE游标与异常处理

    首先,让我们来理解Oracle游标。游标是数据库系统提供的一种机制,允许用户在结果集上进行迭代,一次处理一行数据。在PL/SQL中,游标用于检索SQL查询返回的结果集,并按需逐行处理。以下是一个简单的游标使用示例: ...

    oracle游标优化

    ### Oracle游标优化 在Oracle数据库管理中,游标是一种重要的机制,用于处理查询结果集。游标可以被看作是存储查询结果的一种临时区域,它允许用户通过循环逐行处理这些结果。游标不仅可以提高应用程序的灵活性,还...

Global site tag (gtag.js) - Google Analytics