1
DECLARE
   TYPE EmpList IS TABLE OF varchar2(50) INDEX BY BINARY_INTEGER;

   temp    SYS_REFCURSOR;
   v_temp  varchar2(50);
   v_emp   EmpList;
BEGIN
   v_emp (1) := 'gaurav';
   v_emp (2) := 'manu';

   open temp for select v_emp(level) from dual connect by level<=2;
   loop
    fetch temp into v_temp;
     exit when temp%notfound;
       DBMS_OUTPUT.put_line (v_temp);
    end loop;
   close temp;

--the below part works, then why not the above part dint works
 for i in v_emp.first..v_emp.last
  loop
      dbms_output.put_line(v_emp(i));
  end loop;

EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (SQLERRM);
END;

谁能告诉我为什么这个集合在这里工作??它给了我 no_data_found 异常。

4

2 回答 2

2

失败的原因是

v_emp(level) 

在运行时被评估(作为它的一个变量),所以实际上会对所有行进行评估 v_emp(0);

如果您将数组更改为

v_emp (0) := 'gaurav';
v_emp (1) := 'manu';

正确的方法(如果你不知道)是:

create TYPE EmpList IS TABLE OF varchar2(50);
/

进而 :

v_emp := EmpList('gaurav', 'manu');
open temp for select column_value from table(v_emp);
于 2013-02-07T23:46:18.677 回答
1

试试这个:

DECLARE
   TYPE EmpList IS TABLE OF varchar2(50) INDEX BY BINARY_INTEGER;

   temp    SYS_REFCURSOR;
   v_temp  varchar2(50);
   v_emp   EmpList;
BEGIN
   v_emp (0) := 'gaurav';
   v_emp (1) := 'manu';

   open temp for select v_emp(level) from dual connect by level<=2;
   loop
    fetch temp into v_temp;
        exit when temp%notfound;
       DBMS_OUTPUT.put_line ('v_temp' || v_temp);
    end loop;
   close temp;

--the below part works, then why not the above part dint works
 for i in v_emp.first..v_emp.last
  loop
      dbms_output.put_line(v_emp(i));
  end loop;

EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (SQLERRM);
END;
于 2013-02-08T04:54:36.830 回答