http://topic.csdn.net/u/20080416/11/910e40c1-60f1-441f-8b0f-19a969d30f77.html
这个里面,你的例子是可用的。
我想问下返回游标不定列的时候,
PL/SQL中如何把值显式出来?
JAVA中如何显式出来?
我只会写固定列的。
下面是我写的。我把你例子改成只出来2列的了,1列是XH,1列是剩下的
declare  
id VARCHAR2(10); 
id2 varchar2(100); 
          cursor1         sp_test.ResultData ;  
begin  
        sp_test.getRstData (cursor1);  
        loop  
        fetch   cursor1   into   id,id2;  
            exit   when   cursor1%notfound;  
            dbms_output.put_line(id||'---'||id2);  
        end   loop;  
end;   

解决方案 »

  1.   

    从原贴的问题描述看,并不是一个行列转置的标准问题。
    确切的将是一个分组变幻的问题。
    思路
    CURSOR CSR IS select distinct course from 成绩表 order by course ;
    动态创建临时表
    loop
    colstr := 'sid varchar2(10)';
    loop
    fetch CSR into tmpstr;
    exit when csr%notfound;
    colstr := colstr || temstr ||', varchar2(50) ';
    end loop;
    execute immediate 'CREATE GLOBAL TEMPORARY TABLE T_TEMP (' || 
    colstr || ')ON COMMIT delete ROWS';
    重新按照分组查询成绩表
    rlt 成绩表%rowtype;
    type  Crd is record (ky varchar2(10),val varchar2(100));
    rd Crd;
    type tmprt is table of varchar2(10) index by binary_integer;
    tt tmprt;
    sssid varchar2(10);
    sqltmp varchar2(4000);
    coltmp varchar2(4000);
    valtmp varchar2(4000);
    lp number;
    cursor C1 is select id,sid,course,result from 成绩表 order by sid,course ;
    loop
    fetch c1 into rlt;
    exit when c1%notfound;
    if sssid is null then 
      sssid:=rlt.sid;
      lp :=0;
    else
      if sssid <> rlt.sid then
        sqltmp := 'insert into T_TEMP(';
        coltmp :='sid';
        valtmp := ' values ( ' || sssid;
        loop
          exit when loop < 0;
          coltmp := coltmp || ', ' || rd(lp).ky;
          valtmp := valtmp || ', ' || rd(lp).val;
          lp := lp -1;
        end loop;
        sqltmp = sqltmp || coltmp || ') ' || valtmp || ' ) ';
        execute immediate sqltmp;
        lp:=0;
      else
         rd(lp).ky := course;
         rd(lp).val := result;
         lp := lp +1;
      end if
    end if;
    end loop;
      

  2.   

    3楼的不用管那个问题,直接说怎么调用hongqi162写的那个东西
    显式出来那个结果我在procedure  建表,怎么没权限?直接建表是可以的,缺什么权限啊?
      

  3.   

    应该是这样子的吧..试一下.楼上.--Microsoft Windows XP [版本 5.1.2600]
    --(C) 版权所有 1985-2001 Microsoft Corp.C:\Documents and Settings\molanzhou>sqlplus/nolog--SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 4月 17 16:54:31 2008--Copyright (c) 1982, 2005, Oracle.  All rights reserved.SQL> conn okiwmshistory/okiwmshistory@mytmsdb
    --已连接。
    SQL> var cur refcursor;
    SQL> begin
      2  sp_test.getrstdata(:cur);
      3  end;
      4  /--PL/SQL 过程已成功完成。SQL> print :cur;XH           20080417   20080418   20080419
    ---------- ---------- ---------- ----------
    1                  10         14         23
    2                  21         24          0
    3                  13         22          0SQL>
      

  4.   

    楼上写了,我才发现。
    我一直用的是pl/sql dev的command命令窗口写的,换成sqlplus就好了.
    我用pl/sql dev的command,有错   Cursor variable :CUR cannot be nil我以前一直认为这2个是一样的,今天才发现这个区别。那我想把值用java取出来怎么写?
      

  5.   

    晕阿,在Java里面调用可定时给予racleCallableStatement等类的了,
    类似的例子倒是知道不少。
      

  6.   


    PL/SQL 我也用过了,没有发现异常.
    应该是一样的.SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 4月 17 17:41:02 2008Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    连接到: 
    Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.1.0 - ProductionSQL> var cur refcursor;
    SQL> begin
      2  sp_test.getrstdata(:cur);
      3  end;
      4  /PL/SQL 过程已成功完成。SQL> print :cur;XH           20080417   20080418   20080419
    ---------- ---------- ---------- ----------
    1                  10         14         23
    2                  21         24          0
    3                  13         22          0SQL> 
      

  7.   

    为什么我的pl/sql dev不行SQL> var cur refcursor;
    REFCURSOR not supported
    游标就定义不起来,非要在sqlplus里面
    这个原因是什么?
      

  8.   

    --测试表
    create table curTest(i number);
    --测试数据
    insert into curTest select 1 from dual union all select 2 from dual;
    --测试包
    create or replace package sp_test is
      type ResultData is ref cursor;
      procedure getcurTest(rst out ResultData);
    end sp_test;
    /
    create or replace package body sp_test is
      procedure getcurTest(rst out ResultData) is
      begin
         open rst for select * from curTest;--返回curTest的内容
      end;
    end sp_test;
    /
    --调试包中的getcurTest
    declare
    cur sp_test.ResultData;
    i number;
    begin
      sp_test.getcurTest(cur);
      loop
        fetch cur into i;    
      exit when cur%notfound;
      dbms_output.put_line(i);
      end loop;
    end;
    --输出结果
    1
    2
      

  9.   

    hongqi162,你写的固定列的啊!和我写的差不多啊不定列的怎么写?在sqlplus里面已经解决了,在pl/sql dev里面怎么写?还有就是sqlplus和pl/sql dev有区别吗?已经觉得没区别,现在发现这点区别。定义游标就不一样
      

  10.   


    一起研究下
    declare
     l_cur number;
     l_stmt varchar2(2000);
     l_dtbl dbms_sql.desc_tab;
     l_cnt number;
     l_status number;
     l_val varchar2(200);
    begin
     l_cur := dbms_sql.open_cursor;
     l_stmt := 'select * from curTest';
     dbms_sql.parse(l_cur,'select * from curTest',dbms_sql.native);
     dbms_sql.describe_columns(l_cur,l_cnt,l_dtbl);
     for i in 1..l_cnt loop
      dbms_sql.define_column(l_cur,i,l_val,30);
     end loop;
     l_status := dbms_sql.execute(l_cur);
     while ( dbms_sql.fetch_rows(l_cur) > 0 ) loop
     for i in 1..l_cnt loop
      dbms_sql.column_value(l_cur,i,l_val);
      dbms_output.put_line(l_dtbl(i).col_name||' --> '||l_val);
     end loop;
     end loop;
     dbms_sql.close_cursor(l_cur);
    end;
      

  11.   

    Dynamic Ref Cursor with Dynamic Fetch
    http://www.oracle.com/technology/oramag/code/tips2003/042003.html
      

  12.   

    对的,要的就是这样的效果。
    我英文不好,看不懂那东西....还有就是
    还有就是sqlplus和pl/sql dev有区别吗?
    区别在哪?因为我以前认为没区别
      

  13.   

    pl/sql dev里面有command window
      

  14.   

    我就是在那里面写的,你的例子是可以的。
    还有就是sqlplus和pl/sql dev的command window有区别吗? 我在sqlplus可以var cur refcursor; 
    在pl/sql dev的command window可以