for example:SQL> declare
  2    v_out varchar2(100);
  3  begin
  4   if 1=1 then
  5       declare
  6        cursor c_1 is select 'a' col from dual;
  7       begin
  8        open c_1;
  9        fetch c_1 into v_out;
 10        dbms_output.put_line(v_out);
 11        end;
 12    end if;
 13   end;
 14  /
aPL/SQL 过程已成功完成。SQL>

解决方案 »

  1.   

    SQL> declare
      2    v_out varchar2(100);
      3  begin
      4   if 1>2 then
      5       declare
      6        cursor c_1 is select 'a' col from dual;
      7       begin
      8        open c_1;
      9        fetch c_1 into v_out;
     10        dbms_output.put_line(v_out);
     11        end;
     12   else
     13            declare
     14          cursor c_2 is select 'b' col from dual;
     15       begin
     16        open c_2;
     17        fetch c_2 into v_out;
     18        dbms_output.put_line(v_out);
     19       end;
     20   end if;
     21  end;
     22  /
    bPL/SQL 过程已成功完成。SQL>
      

  2.   

    感谢楼上的回答,但我的procedure只在cursor定义中有些差别,实际的begin .... end 中的内容都是一样的,有什么方法可以把cursor的定义单独的出来,把begin放到end if;后去实际。好像是用包能实现吧,但又不清楚怎么做比较好。
      

  3.   

    这样的话
    使用动态游标ref cursorfor example:SQL> declare
      2    type c is ref cursor;
      3    v_out varchar2(100);
      4    c_1 c;
      5  begin
      6    if 1>2 then
      7     open c_1 for 'select ''a''  col from dual';
      8    else
      9     open c_1 for 'select ''b''  col from dual';
     10    end if;
     11  fetch c_1 into v_out;
     12  dbms_output.put_line(v_out);
     13  end;
     14  /
    bPL/SQL 过程已成功完成。SQL>