create or replace procedure testref as type bcur_t is ref cursor; bcur bcur_t; brec books%rowtype;--books:a table(columnA,columnB) begin open bcur for 'select * from (select a.*,ROWNUM rnum from books a where ROWNUM<6) where rnum>2'; loop fetch bcur into brec; exit when bcur%notfound; dbms_output.put_line(brec.columnA); end loop; close bcur; exception when others then dbms_output.put_line('exception'); end; /
1 'select * from books a where a.ROWNUM<6 and where rnum>2' 这样可以吗2你用loop ,end loop结构,到最后一行没有问题吗(提示属性不存在,我用的不是oracle)?
'select * from books a where a.ROWNUM<6 and a.ROWNUM>2'
loop ,end loop没有问题,我知道了
books 表有两列,所以brec 结构就只能存储两个值,但 select * from (select a.*,ROWNUM rnum from books a where ROWNUM<6) 这个sql却选出3个值。
shi97521601(shi) 说的完全正确你定义的brec结构只有两个值,而在游标里面每个值必须要相互对应,现在你sql的结果是3个所以报错,而你用户查询语句执行,只是显示记录结果,没有对应关系,你有几个值都不重要,所以执行是好的而前面,“我把复杂的 查询语句换成简单的语句就好使:select * from books;”也是因为brec结构值和你查询出结果的值匹配,还有给你个建议在游标里面最好别用*,将你需要的字段直接写出。
as
type bcur_t is ref cursor;
bcur bcur_t;
brec books%rowtype;--books:a table(columnA,columnB)
begin
open bcur for 'select * from (select a.*,ROWNUM rnum from books a where ROWNUM<6) where rnum>2';
loop
fetch bcur into brec;
exit when bcur%notfound;
dbms_output.put_line(brec.columnA);
end loop;
close bcur;
exception
when others then
dbms_output.put_line('exception');
end;
/
where a.ROWNUM<6 and where rnum>2' 这样可以吗2你用loop ,end loop结构,到最后一行没有问题吗(提示属性不存在,我用的不是oracle)?
where a.ROWNUM<6 and a.ROWNUM>2'
select * from (select a.*,ROWNUM rnum from books a where ROWNUM<6)
这个sql却选出3个值。