建表 插入数据
create table test(
id number primary key,
name varchar2(10));insert into test test values(1,'1');
insert into test test values(2,'2');
insert into test test values(3,'3');
insert into test test values(4,'4');
insert into test test values(5,'5');编程:
declare
type t_emp is table of test%rowtype index by binary_integer;
v_cnt binary_integer := 1;
v_emp t_emp;
begin
loop
select *
into v_emp(v_cnt)
from test
where id = v_cnt;
dbms_output.put_line(v_emp(v_cnt).name);
v_cnt := v_cnt + 1;
exit when v_cnt >=3 ;
end loop;
while v_cnt <> 0 loop
dbms_output.put_line(v_cnt);
dbms_output.put_line(v_emp(v_cnt).name);--在sql窗口显示这里有错
v_cnt := v_cnt -1;  --命令窗口显示这里有错
end loop;
end;错误信息
sql窗口报的错
ORA-01403: 未找到数据
ORA-06512: 在 line 17
命令窗口报的错
ORA-01403: 未找到数据
ORA-06512: 在 line 18

解决方案 »

  1.   

    select * 
    into v_emp(v_cnt) 
    from test 
    where id = v_cnt; 这里加begin...exception...end试一下
      

  2.   

    v_cnt := v_cnt + 1; 
    exit when v_cnt >=3 ; 
    end loop; 
    while v_cnt <> 0 loop 
    dbms_output.put_line(v_cnt); 
    dbms_output.put_line(v_emp(v_cnt).name);--在sql窗口显示这里有错你这里的v_cnt+1了,所以dbms_output.put_line(v_emp(v_cnt).name)这里是找不到数据的。
    如果修改为:
    declare 
    type t_emp is table of test%rowtype index by binary_integer; 
    v_cnt binary_integer := 1; 
    v_emp t_emp; 
    begin 
    loop 
    select * 
    into v_emp(v_cnt) 
    from test 
    where id = v_cnt; 
    dbms_output.put_line(v_emp(v_cnt).name); 
    v_cnt := v_cnt + 1; 
    exit when v_cnt >=3 ; 
    end loop; 
    v_cnt:=v_cnt-1;
    while v_cnt <> 0 loop 
    dbms_output.put_line(v_cnt); 
    dbms_output.put_line(v_emp(v_cnt).name);--在sql窗口显示这里有错 
    v_cnt := v_cnt -1;  --命令窗口显示这里有错 
    end loop; 
    end; 这样就可以执行通过了