begin      open p_cursor for select ename, empno from emp order by ename;end;With 7.2 on up of the database you have cursor variables.  Cursor variables are cursors opened by a pl/sql routine and fetched from by another application or pl/sql routine (in 7.3 pl/sql routines can fetch from cursor variables as well as open them). The cursor variables are opened with the privelegs of the owner of the procedure and behave just like they were completely contained within the pl/sql routine. It uses the inputs to decide what database it will run a query on. Here is an example: 
  create or replace package types 
as 
    type cursorType is ref cursor; 
end; 
/ create or replace function sp_ListEmp return types.cursortype 
as 
    l_cursor    types.cursorType; 
begin 
    open l_cursor for select ename, empno from emp order by ename;     return l_cursor; 
end; 
/
create or replace procedure getemps( p_cursor in out types.cursorType )asbegin      open p_cursor for select ename, empno from emp order by ename;end;/

解决方案 »

  1.   

    create package text_age
    as
    type t_sor is ref cursor;
    end;
    /
    create procedure pro(p_rc out text_age.t_sor)
    as
    str varchar2(50);
    begin
    str:='select * from a';
    open p_rc for str;
    end;
    /
      

  2.   

    得到的游标变量,是不是只能用在别的存储过程中?能直接得到结果集吗?
    例如,调用存储过程getResults(XXX),一次直接得到类似“select * from a”的结果集
      

  3.   

    请问一下,这样的结果集能否在java程序里面通过调用该存储过程直接得到?可以的话能否给演示一下:D谢谢先
      

  4.   

    调用存储过程getResults(XXX),一次直接得到类似“select * from a”的结果集
    当然可以了,beckhambobo说的就是这个意思这样的结果集能否在java程序里面通过调用该存储过程直接得到?
    应该是可以的!
    CREATE OR REPLACE PACKAGE BODY "PKG_TEST"
    AS
       PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)
       IS
          sqlstr   VARCHAR2 (500);
       BEGIN
          IF p_id = 0 THEN
             sqlstr := 'SELECT ID, NAME, sex, address, postcode, birthday
                  FROM NB_TEST_SQL';
             OPEN p_rc FOR sqlstr;
                
          ELSE
             sqlstr :=
                'select id,name,sex,address,postcode,birthday
               from NB_TEST_SQL where id=:w_id';
             OPEN p_rc FOR sqlstr USING p_id;
          END IF;
       END get;
    END pkg_test;JAVA里调用:
    public ResultSet ShowContent() 

    {

    try{

    connect = DriverManager.getConnection(sConnStr,"water","water");
    CallableStatement cstmt = connect.prepareCall("{?=call PKG_TEST.get(?,?)}");

        cstmt.setInt(1,1);

        cstmt.executeQuery();
        rs = cstmt.getResultSet();
        
            
      }
    catch(SQLException ex){
    System.err.println(ex.getMessage());
    }

    return rs;
    }
      

  5.   

    谢谢Strawberry79(永远的草莓地!)
    可是我尝试了一下,报了如下的错误:
    java.sql.SQLException: ORA-01008: 并非所有变量都已关联
    请问一下在java中调用存储过程前是否需要对输出参数进行注册
    :D