SQL> select * from test;        T1         T2         T3 A1
---------- ---------- ---------- --------
         1          2          3 123create or replace procedure myproc(flag in number)
as
type my_cursor is ref cursor;
select_cur my_cursor;
str1 varchar2(200);
str2 varchar2(50);
v1   test.t1%type;
v2   test.t2%type;
begin
 case flag
      when 0 then str1:='select t1,t2 from test';
      when 1 then str1:='select t1,t3 from test';
      else        str1:='select t2,t3 from test';
 end case;
 open select_cur for str1;
 loop
   fetch select_cur into v1,v2;
   exit when select_cur%notfound;
   dbms_output.put_line(v1+v2);
 end loop;
 close select_cur;
end;
/SQL> exec myproc(1)
4PL/SQL 过程已成功完成。============================
你的游标fetch到哪个变量名都一样的,所有没有必要取不同的名字
只要你的t1,t2,t3的数据类型一致即可

解决方案 »

  1.   

    谢谢,我刚才举的例子也许不好,我说的select t1,..tn from test中的字段个数是不一定的,也不能肯定数据类型是一样的,具体的取值是从表test中根据不同的条件取出不同的字段组合,所以才想到用上述方法的。
      

  2.   

    create or replace procedure myproc(flag in number)
    as
    type my_cursor is ref cursor;
    select_cur my_cursor;
    str1 varchar2(200);
    my_record test%rowtype;
    begin
     case flag
          when 0 then str1:='select t1,t2 from test';
          when 1 then str1:='select t1,t3 from test';
          else        str1:='select t2,t3 from test';
     end case;
     open select_cur for str1;
     loop
       fetch select_cur into my_record;
       exit when select_cur%notfound;
       dbms_output.put_line(my_record.t1||' '||my_record.t2||' '||my_record.t3);
     end loop;
     close select_cur;
    end;
    /SQL> select * from test;        T1         T2         T3 A1
    ---------- ---------- ---------- --------
             1          2          3 123SQL> exec myproc(0)
    1 2PL/SQL 过程已成功完成。SQL> exec myproc(1)
    1 3PL/SQL 过程已成功完成。SQL> exec myproc(2)
    2 3PL/SQL 过程已成功完成。
      

  3.   

    高手!!谢谢!
    再请教:test表有三个字段,而str1每次只取两个不同的字段,fetch select_cur into my_record;语句可以自动对应t1、t2或t3吗?麻烦给解释解释,让我长长知识,谢谢!
      

  4.   

    ATGC写的已经是test表中有三个字段t1,t2,t3了啊,而且是每次只取两个不同的字段。如果要再增加字段的话,也只是增加字段组合的方式,多加几个when ... then ...对应每种组合就行了啊
      

  5.   

    实际上是这样的
    fetch select_cur into my_record;
    由于你的t1,t2,t3都是一样的
    只要这样就可以了,因为每次你只是取出两个字段
    dbms_output.put_line(my_record.t1||' '||my_record.t2);你说的没错。。每次fetch都会依次对应my_record每个字段
    要注意的是每次fetch是两个字段,
    我的my_record是你的表的一个记录类型
    相当于一次性定义了下面的四次定义
     test.t1%type
     test.t2%type
     test.t3%type
     test.a1%type当你select t1,t3的时候,会把t3赋值给my_record.t2
    但是由于你的t1,t2,t3是一样的,所以没有关系
    如果你是select *  from test
    那么忽会依次填满my_record
      

  6.   

    哪是要求这几个字段的数据类型要一致才行啊,对我的要求有的不太相符,因为我的字段类型是有可能不同的,所以要根据不同的条件将select出来的字段赋予不同的变量。
      

  7.   

    这样变通就可以了
    case flag
          when 0 then str1:='select t1,t2,'','' from test';
          when 1 then str1:='select t1,'',t3,'' from test';
          else        str1:='select '',t2,t3,'' from test';
     end case;那就一一对应了。。
      

  8.   

    呵呵,你的写法是假设select三个字段的前提下,实际中是没有这种可能的,比如:
    case flag
          when 0 then str1:='select t1,t2,t3 from test';
          when 1 then str1:='select t2,t5 from test';
          when 2 then str1:='select t1,t2,t8,t9 from test;
          when 3 then str1:='select t2 from test';
    end case;
    又该如何?所以我才会想能否这样写:(写法肯定不对,只是想表达意思)
    case flag
          when 0 then str1:='select t1,t2,t3 from test';
                    v_str1:='v_t1,v_t2,v_t3'; 
          when 1 then str1:='select t2,t5 from test';
                    v_str1:='v_t2,v_t5';
          when 2 then str1:='select t1,t2,t8,t9 from test; 
                    v_str1:='v_t1,v_t2,v_t8,v_t9';
          when 3 then str1:='select t2 from test';
                    v_str1:='v_t2';
    end case;
    .....
    while
       ......
      fetch str1 into v_str1;
       .....
    loop
      

  9.   

    create or replace procedure myproc(flag in number)
    as
    type my_cursor is ref cursor;
    select_cur my_cursor;
    str1 varchar2(200);
    str2 varchar2(30000);
    begin
     case flag
          when 0 then str1:='select t1||'' ''||t2 from test';
          when 1 then str1:='select t1||'' ''||t3 from test';
          else        str1:='select t2||'' ''||t3 from test';
     end case;
     open select_cur for str1;
     loop
       fetch select_cur into str2;
       exit when select_cur%notfound;
       dbms_output.put_line(str2);
     end loop;
     close select_cur;
    end;
    /SQL> select * from test;T1                 T2         T3 A1
    ---------- ---------- ---------- --------
    30-11月-04          1         10 aaaSQL> edit car.sqlSQL> exec myproc(0)
    30-11月-04 1PL/SQL 过程已成功完成。SQL> exec myproc(1)
    30-11月-04 10PL/SQL 过程已成功完成。SQL> exec myproc(2)
    1 10PL/SQL 过程已成功完成。
      

  10.   

    谢谢,str1:='select t1||'' ''||t2 from test';写法可以吗?有点看不懂,我对Oracle了解不够,请多指教,另:如果要返回一个数据集该如何写?
      

  11.   

    可以
    这本来就是一个数据集
    只不过现在是把所有字段合并在一起。。用空格分隔每个字段的内容
    其实我不知道你具体使用时要达到怎样的效果
    我也没有时间帮你想了。。你如果要在应用程序端返回结果集,那只要一句SQL语句即可。。无需这样复杂。。