--利用9i出来的sys_refcursor
create or REPLACE PROCEDURE proc_get(p_rc out sys_refcursor) as
BEGIN
open p_rc for select table_name,tablespace_name,status from user_table_t;
END;
/
VARIABLE p_rc refcursor;
EXEC proc_get(:p_rc);
print;
--9i之前是要定义游标变量的
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get (p_rc OUT myrctype);
END pkg_test;
/
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get (p_rc OUT myrctype)
IS
sqlstr VARCHAR2 (500);
BEGIN
OPEN p_rc FOR
select table_name,tablespace_name,status from user_table_t;
END get;
END pkg_test;
/
VARIABLE p_rc refcursor;
exec pkg_test.get(:p_rc);
print p_rc;
--利用游标循环输出
set serveroutput on
declare
type mytable is table of user_table_t%rowtype;
l_data mytable;
l_refc sys_refcursor;
BEGIN
open l_refc for select table_name,tablespace_name,status from user_table_t;
fetch l_refc BULK COLLECT into l_data;
CLOSE l_refc;
FOR i IN 1 .. l_data.COUNT
LOOP
DBMS_OUTPUT.put_line ( l_data (i).table_name ¦ ¦'`s TABLESPACE is ' ¦ ¦l_data(i).tablespace_name);
END LOOP;
END;
/