日期:2014-05-17  浏览次数:20778 次

怎么找到隐式cursor最后一条记录
for c in (select * from student) loop
我想取到最后一条记录。怎么求?

------解决方案--------------------
定义个recored记录:
declare
...
l_r student%ROWTYPE;
...
begin
for c in (select * from student) loop 
...
l_r:=c;
end loop;
--l_r就是最后的记录,这里处理
....
end;
/

引用楼主 cinderella_m 的帖子:
for c in (select * from student) loop
我想取到最后一条记录。怎么求?

------解决方案--------------------
SQL code
-- 换一种思路可以用可变数组:
   set serveroutput on
   declare
        type tabletype1 is table of varchar2(9) index by binary_integer;
        table1 tabletype1;
   begin
        table1(1):='成都市';
        table1(2):='北京市';
        table1(3):='青岛市';
        dbms_output.put_line('总记录数:'||to_char(table1.count));
        dbms_output.put_line('第一条记录:'||table1.first);
        dbms_output.put_line('最后条记录:'||table1.last);
        dbms_output.put_line('第二条的前一条记录:'||table1.prior(2));
        dbms_output.put_line('第二条的后一条记录:'||table1.next(2));
    end;

------解决方案--------------------
SQL code
SQL> set serveroutput on;
SQL> 
SQL> declare
  2    cursor cu is
  3      select ename from emp where deptno = 10;
  4    type enametype is table of varchar2(10);
  5    etype enametype;
  6  begin
  7    open cu;
  8    fetch cu bulk collect
  9      into etype;
 10    dbms_output.put_line(etype(cu%rowcount));
 11    close cu;
 12  end;
 13  /

MILLER

PL/SQL procedure successfully completed