oracle中如何得到游标的列数,我在表里select一个sql语句,然后执行,但是我不知道他返回的字段个数,没办法fetch into给变量,能事先得到游标返回的字段个数吗,是用plsql写的,能fetch into 给一个数组吗?
请各位高手指点指点!

解决方案 »

  1.   

    你定义游标的时候就知道列数了没有指定列的话,先count 一下你的字段
      

  2.   


    declare
        cursor cur_test is select * from dual;
        list   cur_test%rowtype;
    begin
        open cur_test;
        fetch cur_test into list;
        dbms_output.put_line(list.dummy);
    end;
    /
      

  3.   


    fetch给数组?没听说过,但是可以fetch给自定义记录类型。游标长度不知道怎么获得,定义游标的时候应该知道列数及列名了啊SQL> declare
      2      cursor cur_test is select id,name from a;
      3      type t_a is record(
      4     id1 a.id%TYPE,
      5     name1 a.name%TYPE
      6      );
      7      rec_a t_a;
      8  begin
      9      open cur_test;
     10      loop
     11         fetch cur_test into rec_a;
     12         exit when cur_test%notfound;
     13         dbms_output.put_line(rec_a.id1||':'||rec_a.name1);
     14      end loop;
     15      close cur_test;
     16  end;
     17  /
    1:甲
    2:乙
    3:丙
    4:丁PL/SQL 过程已成功完成。
      

  4.   

    但如果游标是
    cursor v_cursor is select * from a;呢?
    而a的字段又比较多呢?
      

  5.   

    我怎么不能fetch到varray啊create table a (id number(10),name varchar2(10));
    insert into a  select 1,'甲' from dual union select 2,'乙' from dual;
    insert into a  select 3,'丙' from dual union select 4,'丁' from dual;declare
        cursor cur_test is select to_char(id) id,name from a;
        type t_a is varray(2) of varchar2(10);
        rec_a t_a;
    begin
        open cur_test;
        loop
        fetch cur_test into rec_a;
        exit when cur_test%notfound;
        dbms_output.put_line(rec_a(1)||':'||rec_a(2));
        end loop;
        close cur_test;
    end;
    /
    ERROR 位于第 8 行:
    ORA-06550: line 8, column 26:
    PLS-00597: expression 'REC_A' in the INTO list is of wrong type
    ORA-06550: line 8, column 6:
    PL/SQL: SQL Statement ignored
      

  6.   


    [TEST@ora10gr1#2009-12-18/17:32:17] SQL>select * from t1;        ID NAME
    ---------- ----------
             1 aaa
             2 bbb
             3 ccc[TEST@ora10gr1#2009-12-18/17:32:26] SQL>set serveroutput on
    [TEST@ora10gr1#2009-12-18/17:32:28] SQL>declare
      2     type  t_type is table of t1%rowtype;
      3     t_rec t_type;
      4  begin
      5     select * bulk collect into t_rec from t1;
      6     for i in t_rec.first..t_rec.count loop
      7         dbms_output.put_line(t_rec(i).id||t_rec(1).name);
      8     end loop;
      9  end;
     10  /
    1aaa
    2aaa
    3aaaPL/SQL procedure successfully completed.[TEST@ora10gr1#2009-12-18/17:32:28] SQL>
      

  7.   

    修改下 把 i 写成 1 了。
    [TEST@ora10gr1#2009-12-18/17:33:53] SQL>declare
      2     type  t_type is table of t1%rowtype;
      3     t_rec t_type;
      4  begin
      5     select * bulk collect into t_rec from t1;
      6     for i in t_rec.first..t_rec.count loop
      7         dbms_output.put_line(t_rec(i).id||t_rec(i).name);
      8     end loop;
      9  end;
     10  /
    1aaa
    2bbb
    3cccPL/SQL procedure successfully completed.
      

  8.   

    直接取你要查询的表的列不就行了么?
    SELECT OWNER,TABLE_NAME,COLUMN_NAME FROM DBA_TAB_COLUMNS
      

  9.   

    好像大家回答的都不是楼主想要的吧!!!
    lz想要的是:得到一个游标其包含的字段个数(比如:cursor cur_test is select deptno,comm from scott.dept,楼主可能是想要得到该游标包含的字段个数是2!)。
      

  10.   

    顶 不太明白,呵呵,如果是从一个表中,直接取列,如果是一个动态的SELECT, 肯定定义了SELECT 的字段啊,所过游标取出的值是 SELECT * 的话,额,先全部建成临时表再取列吧?
      

  11.   

    --将table的列数赋值给变量a
    select count(*)  into a from   user_tab_cols  where table_name= upper('table');
    这样就可以了 
      

  12.   

    呵呵 
    思考一下
    如果只知道游标名
    游标的定义你是看不到的
    那么如何查看这个游标的select语句中返回了哪些列和列数呢?