Oracle中游标Cursor的用法详解


当前第2页 返回上一页

1.使用游标FOR循环

--不需要声明v_bookname,Open和Close游标和fetch操作(不用打开游标和关闭游标,实现遍历游标最高效方式)
declare
 cursor c_book(i_id number) is select bookname from book where id = i_id;
begin
   for cur in c_book(10) loop --循环变量cur不需要声明
     update book set price = '53' where bookname = cur.bookname;
   end loop;
end;

2.在游标FOR循环中直接使用子查询

begin
     for emp_record in (select ename,sal from emp) loop
        dbms_output.put_line(emp_record.ename);
    end loop;
end;

三、使用游标更新或删除数据

要通过游标更新或删除数据,在定义游标时必须要带有FOR UPDATE子句

cursor cursor_name(parameter_name datetype) is select_statement for update [of column_reference] [nowait];
  • for update子句:用于在游标结果集数据上家行共享锁,防止其他用户在相应行执行DML操作
  • of子句:确定哪些表要加锁,没有OF子句,则在所引用的全部表上加锁
  • nowait子句:用于指定不等待锁
  • 必须在UPDATE后DELETE语句中引用WHERE CURRENT OF子句
    update table_name set column=.. where current of cursor_name;
    delete table_name where current of cursor_name;
declare
    cursor emp_cursor is select ename,sal from emp for update;
    v_ename emp.ename%type;
    v_sal emp.sal%tyep;
  begin
    open emp_cursor;
    loop
     fetch emp_cursor into v_ename,v_oldsal;
     exit when emp_cursor%notfound;
     if v_oldsal<2000 then
        update emp set sal=sal+100 where current of emp_cursor;--delete from emp where current of emp_cursor;
     end if;
   end loop;
   close emp_cursor;
 end;

四、通过bulk collect减少loop处理的开销

将查询结果一次性加载到集合中,而不是一条一条的加载。

(1)在显示游标中,使用FETCH..BALK 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;

(2)游标中使用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.pur('雇员名');
     for i in 1..(emp_currsor%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;

五、使用游标变量

PL/SQL的游标变量中存放着指向内存地址的指针.

1.游标变量使用步骤

包括定义游标变量,打开游标,提取游标数据,关闭游标等四个阶段

1.1定义ref cursor类型和游标变量

type ref_type_name is ref cursor [return return_type];

cursor_varibale ref_type_name;

当指定RETURN子句时,其数据类型必须是记录类型,不能在包内定义游标变量

1.2打开游标

open cursor_variable for select_statement;

1.3提取游标数据

fetch cursor_varibale into variable1,variable2,...;

fetch cursor_varibale bulk collect into collect1,collect2,...[limit rows]

1.4关闭游标变量

close cursor_varibale;

2.游标变量使用示例

1、在定义FEF CURSOR类型时不指定RETURN子句

在打开游标时可以指定任何的SELECT语句

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=10;
    loop
     fetch emp_cursor into emp_record;
     exit when emp_cursor%notfound;
     dbms_output.put_line('第'||emp_curosr%rowcount||'个雇员: '||emp_record.ename);
    end loop;
    close emp_cursor;
  end;

2、在定义REF CURSOR类型时指定RETURN子句

在打开游标时SELECT语句的返回结果必须与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=20;
    loop
     fetch emp_cursor into emp_record;
     exit when emp_cursor%notfound;
     dbms_output.put_line('第'||emp_curosr%rowcount||'个雇员: '||emp_record.ename);
    end loop;
    close emp_cursor;
 end;

到此这篇关于Oracle中游标Cursor用法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...