`

精通Oracle10编程SQL(9)使用游标

阅读更多
/*
 *使用游标
 */
--显示游标
--在显式游标中使用FETCH...INTO语句
DECLARE
   CURSOR emp_cursor is 
      select ename,sal from emp where deptno=1;
   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;

SELECT * FROM emp;

--在显示游标中,使用FETCH...BULK COLLECT INTO语句提取所有数据
DECLARE
  CURSOR emp_cursor is
     select ename from emp where deptno=1;
  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语句提取部分数据
--下面以每次提取5行数据为例,说明使用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('雇员名:');
      DBMS_OUTPUT.put_line('影响行数'||emp_cursor%ROWCOUNT);
      dbms_output.put_line(v_count);
      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;

--使用游标属性
DECLARE
   CURSOR emp_cursor is select ename from emp where deptno=1;
   TYPE ename_table_type is table of varchar2(10);
   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;
   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;
     exit when emp_cursor%NOTFOUND;
     dbms_output.put_line('雇员名:'||emp_record.ename||',雇员工资:'||emp_record.sal);
  end loop;
  close emp_cursor;
end;

--参数游标
--参数游标是指带有参数的游标,定义参数游标时,需要指定参数名及其数据类型
--注意:定义参数游标时,游标参数只能指定数据类型,而不能指定长度。另外,定义参数游标时,一定要在游标子查询的WHERE子句中引用该参数,否则失去了定义参数游标的意义
DECLARE
   CURSOR emp_cursor(no NUMBER) IS 
      SELECT ename from emp where deptno=no;
   v_ename emp.ename%TYPE;
begin
   open emp_cursor(1);
   loop
     fetch emp_cursor into v_ename;
     exit when emp_cursor%NOTFOUND;
     dbms_output.put_line(v_ename);
   end loop;
   close emp_cursor;
end;

--使用游标更新或删除数据
--使用游标更新数据
DECLARE
  CURSOR emp_cursor is 
     select ename,sal from emp for update;
  v_ename emp.ename%TYPE;
  v_oldsal emp.ename%TYPE;
begin
  open emp_cursor;
  loop
     fetch emp_cursor into v_ename,v_oldsal;
     exit when emp_cursor%NOTFOUND;
     if v_oldsal<1000 then
        update emp set sal=sal+100 where current of emp_cursor;
     end if;
  end loop;
  close emp_cursor;
end;

select * from emp;

--使用游标删除数据
--下面以解雇部门30的所有雇员为例,说明使用显式游标删除数据的方法
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 = 3 then
        delete from emp where current of emp_cursor;
     end if;
  end loop;
  close emp_cursor;
end;

--使用OF子句在特定表上加上行共享锁
--如果游标子查询涉及到多张表,那么在默认情况下会在所有修改表行上加行共享锁。
--为了只在特定表上加行共享锁,需要在FOR UPDATE子句后带有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=1 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;
  close emp_cursor;
END;

--使用NOWAIT子句
--使用FOR UPDATE语句对被作用行加锁,如果其他会话已经在被作用行上加锁,那么在默认情况下当前会话会要一直等待对方释放锁
--通过在FOR UPDATE子句中指定NOWAIT语句,可以避免等待锁。
--当指定了NOWAIT子句之后,如果其他会话已经在被作用行加锁,那么当前会话会显示错误提示信息,并退出PL/SQL块
DECLARE
   CURSOR emp_cursor is 
      select ename,sal from emp for update nowait;
   v_ename emp.ename%TYPE;
   v_oldsal emp.sal%TYPE;
begin
   open emp_cursor;
   loop
      fetch emp_cursor into v_ename,v_oldsal;
      exit when emp_cursor%NOTFOUND;
      if v_oldsal<1000 then
         update emp set sal=sal+100 where current of emp_cursor;
      end if;
   end loop;
   close emp_cursor;
end;

select * from emp;

--游标FOR循环
--使用游标FOR循环
--以顺序显示EMP表的所有雇员为例,说明使用游标FOR循环的方法
DECLARE 
   CURSOR emp_cursor IS SELECT ename,sal from emp;
BEGIN
   for emp_record in emp_cursor loop
      dbms_output.put_line('第'||emp_cursor%ROWCOUNT||'个雇员:'||emp_record.ename);
   end loop;
END;

--在游标FOR循环中直接使用子查询
--如果在使用游标FOR循环时不需要使用任何游标属性,那么可以直接在游标FOR循环中使用子查询
--下面以显示EMP表的所有雇员名为例,说明在游标FOR循环中直接使用子查询的方法
BEGIN
   FOR emp_record in 
      (select ename,sal from emp) loop
      dbms_output.put_line(emp_record.ename);
   end LOOP;
END;

--使用游标变量
--在定义REF CURSOR类型时不指定RETURN子句
DECLARE
   TYPE emp_cursor_type is ref cursor;
   emp_cursor emp_cursor_type;
   emp_record emp%ROWTYPE;
begin
   open emp_cursor for select * from emp where deptno=1;
   loop
      fetch emp_cursor into emp_record;
      exit when emp_cursor%NOTFOUND;
      DBMS_OUTPUT.PUT_LINE('第'||emp_cursor%ROWCOUNT||'个雇员:'||emp_record.ename);
   end loop;
   close emp_cursor;
end;

--在定义REF CURSOR类型时指定RETURN子句
--如果在定义REF CURSOR类型时指定了RETURN子句,在打开游标时SELECT语句的返回结果必须与RETURN子句所指定的记录类型相匹配
--下面以顺序地显示部门20的所有雇员名称为例,说明使用游标变量(包含RETURN子句)的方法
DECLARE
  TYPE emp_record_type is record(
     name VARCHAR2(10),salary NUMBER(6,2));
  type emp_cursor_type is ref cursor
     return emp_record_type;
  emp_cursor emp_cursor_type;
  emp_record emp_record_type;
BEGIN
  open emp_cursor for select ename,sal from emp where deptno=2;
  loop
    fetch emp_cursor into emp_record;
    exit when emp_cursor%NOTFOUND;
    dbms_output.put_line('第'||emp_cursor%ROWCOUNT||'个雇员:'||emp_record.name||',工资:'||emp_record.salary);
  end loop;
  close emp_cursor;
END;

--使用CURSOR表达式
--CURSOR表达式用于返回嵌套游标
--通过使用CURSOR表达式,开发人员可以在PL/SQL块中处理更加复杂的基于多张表的关联数据
--为了在PL/SQL块中取得嵌套游标的数据,需要使用嵌套循环
--下面以显示部门名、雇员名和工资为例,说明在PL/SQL块中使用CURSOR表达式的方法
DECLARE
   TYPE refcursor is ref cursor;
   cursor dept_cursor(no number) is 
      select a.dname,cursor(select ename,sal from emp where deptno=a.deptno) from dept a where a.deptno=no;
   empcur refcursor;
   v_dname dept.dname%TYPE;
   v_ename emp.ename%TYPE;
   v_sal emp.sal%TYPE;
begin
   open dept_cursor(&no);
   loop 
     fetch dept_cursor into v_dname,empcur;
     exit when dept_cursor%NOTFOUND;
     dbms_output.put_line('部门名:'||v_dname);
     loop
       fetch empcur into v_ename,v_sal;
       exit when empcur%NOTFOUND;
       DBMS_OUTPUT.put_line('雇员名:'||v_ename||',工资:'||v_sal);
     end loop;
   end loop;
   close dept_cursor;
end;

select * from emp;
select * from dept;

 

分享到:
评论

相关推荐

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一)

     本书是专门为oracle应用开发人员提供的sql和pl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法...

    精通Oracle.10g.PLSQL编程

    br&gt;精通Oracle 10g PL/SQL编程 &lt;br&gt; 【作 者】王海亮 林立新 于三禄 郑建茹 【丛 书 名】 万水Oracle技术丛书 &lt;br&gt;http://images.china-pub.com/ebook20001-25000/21975/shupi.jpg&lt;br&gt;&lt;br&gt;PL/SQL是...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二)

     本书是专门为oracle应用开发人员提供的sql和pl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法...

    Oracle 11g SQL和PL SQL从入门到精通〖送源代码〗

    本书是专门为Oracle应用开发人员提供的SQL和PL/SQL编程指南。通过学习本书,读者不仅可以掌握Oracle常用工具Oracle Universal Installer、Net Comfiguration Assistant、SQL Developer、SQL*Plus的作用及使用方法,...

    Oracle 11g SQL和PL SQL从入门到精通.part1

     本书是专门为oracle应用开发人员提供的sql和pl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法...

    Oracle 11g SQL和PL SQL从入门到精通part2 pdf格式电子书 下载(二)

     本书是专门为oracle应用开发人员提供的sql和pl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法...

    精通Oracle.10g.Pl.SQL编程

    本书包括:PL/SQL 综述、PL/SQL 开发工具、PL/SQL 基础、使用SQL语句、SQL函数、访问Oracle 、编写控制结构、使用复合数据类型、使用游标、处理例外、开发子程序等内容。 作者:王海亮 出版社:中国水利水电出版社

    Oracle11g从入门到精通2

    第4章 Oracle PL/SQL语言及编程 4.1 PL/SQL简介 4.1.1 PL/SQL的基本结构 4.1.2 PUSQL注释 4.1.3 PL/SQL字符集 4.1.4 PL/SQL数据类型 4.1.5 PIJSQL变量和常量 4.1.6 PL/SQL语句控制结构 4.1.7 PL/...

    Oracle11g从入门到精通

    第4章 Oracle PL/SQL语言及编程 4.1 PL/SQL简介 4.1.1 PL/SQL的基本结构 4.1.2 PUSQL注释 4.1.3 PL/SQL字符集 4.1.4 PL/SQL数据类型 4.1.5 PIJSQL变量和常量 4.1.6 PL/SQL语句控制结构 4.1.7 PL/SQL表达式 ...

    精通sql结构化查询语句

    以SQL Server为工具,讲解SQL语言的应用,提供了近500个曲型应用,读者可以随查随用,深入讲解SQL语言的各种查询语句,详细介绍数据库设计及管理,详细讲解存储过程、解发器和游标等知识,讲解了SQL语言在高级语言中...

    Oracle.11g.从入门到精通 (2/2)

    第4章 Oracle PL/SQL语言及编程 4.1 PL/SQL简介 4.1.1 PL/SQL的基本结构 4.1.2 PUSQL注释 4.1.3 PL/SQL字符集 4.1.4 PL/SQL数据类型 4.1.5 PIJSQL变量和常量 4.1.6 PL/SQL语句控制结构 4.1.7 PL/SQL表达式 4.2 PL/...

    Oracle.11g.从入门到精通 (1/2)

    第4章 Oracle PL/SQL语言及编程 4.1 PL/SQL简介 4.1.1 PL/SQL的基本结构 4.1.2 PUSQL注释 4.1.3 PL/SQL字符集 4.1.4 PL/SQL数据类型 4.1.5 PIJSQL变量和常量 4.1.6 PL/SQL语句控制结构 4.1.7 PL/SQL表达式 4.2 PL/...

    SQL必知必会(第3版-PDF清晰版)part1

    推荐:学习SQL编程,必备书籍,从入门到进阶/精通,实例与理论同步,比较优秀作品!(共分压5部分)目录: 第1章 了解SQL... 1 1.1 数据库基础..... 1 1.1.1 什么是数据库..... 2 1.1.2 表..... 2 1.1.3 列和...

Global site tag (gtag.js) - Google Analytics