I have a table named Table1. It has lots of columns, one of them is Column1. I don't know the other columns, they may even change sometimes. There is a strongly typed ref cursor type which returns Table1%rowtype, named cur_Table1. I have a stored procedure named SP1 which has an out parameter of type cur_Table1. I'm calling this SP1 stored procedure from another database that only sees this stored procedure, but not the table or the type itself. How do I select only Column1 from the returned cursor? I know I can fetch into a record or as many variables as the cursor has columns, but I only know of one column's existence so I can't declare the complete record or correct number of variables.
Oracle – select a specific column from a ref cursor
cursororaclestored-procedures
Related Solutions
Declare t_FeeRecord
and t_Fees
as database objects, not pl/sql objects,
in Oracle PL/SQL types cannot be used in SQL queries, this gives you datatype error (however, this restriction is removed in Oracle 12c).
t_FeeRecord
must be created as object type, not a record type, since records are PL/SQL types and cannot be used in SQL queries.
create type t_FeeRecord is object(
description varchar2(80),
amount number(12,2),
taxAmount number(12,2)
);/
create type t_Fees as table of t_FeeRecord; /
Here is a simple demo that creates a table of records, open a ref cursor for this table and reads the cursor and inserts rows retrieved from the cursor to the table (tested on 11.2g):
create type t_FeeRecord is object(
description varchar2(80),
amount number(12,2),
taxAmount number(12,2)
);
/
create type t_Fees as table of t_FeeRecord;
/
create table temporary_fee(
description varchar2(80),
amount number(12,2),
taxAmount number(12,2)
);
declare
fees t_Fees;
TYPE rctl IS REF CURSOR;
cur rctl;
rec TEMPORARY_FEE%ROWTYPE;
begin
fees := t_Fees (
t_FeeRecord( 'aaa', 20, 30 ),
t_FeeRecord( 'bbb', 10, 76 ),
t_FeeRecord( 'xxx', 4, 23 ),
t_FeeRecord( 'zzz', 7, 43 ),
t_FeeRecord( 'ccc', 13, 44 ) );
open cur for
select * from table( fees );
LOOP
FETCH cur INTO rec;
EXIT WHEN cur%NOTFOUND;
INSERT INTO TEMPORARY_FEE VALUES rec;
END LOOP;
close cur;
end;
/
select * from temporary_fee;
DESCRIPTION AMOUNT TAXAMOUNT
------------ ---------- ----------
aaa 20 30
bbb 10 76
xxx 4 23
zzz 7 43
ccc 13 44
if you want to write a java code which will call PostgreSQL Stored procedure where
there is an INOUT refcursor.
PostgreSQL SP:
CREATE OR REPLACE PROCEDURE Read_DataDetails(INOUT my_cursor REFCURSOR = 'rs_resultone')
LANGUAGE plpgsql
AS $$
BEGIN
OPEN my_cursor FOR Select * from MyData;
END;
$$;
corresponding java code will be:
connection = getConnection(your connection parameter);
if (connection != null) {
connection.setAutoCommit(false); // This line must be written just after the
//connection open otherwise it will not work since cursor will be closed immediately.
String readMetaData = "CALL Read_DataDetails(?)";
callableStatement = connection.prepareCall(readMetaData);
callableStatement.registerOutParameter(1, Types.REF_CURSOR);
callableStatement.execute();
rs = (ResultSet) callableStatement.getObject(1);
if (rs != null) {
while (rs.next()) {
<Your Logic>
}//end while
}//end if
}//end if
Best Solution
You can do this with
DBMS_SQL
, but it ain't pretty.Table and sample data (COLUMN1 has the numbers 1 - 10):
Package with a procedure that opens a ref cursor and selects everything:
PL/SQL block that reads COLUMN1 data from the ref cursor: