Postgres provides cursor expressions, but the syntax is a little less convenient than Oracle.
First you need to create a function to convert to refcursor:
create or replace function arr2crs(arr anyarray) returns refcursor as $$ declare crs refcursor; begin open crs for select * from unnest(arr); return crs; end; $$ language plpgsql volatile;
Now create some test data
create table dep as select 1 depid, 'Sales' depname union all select 2 depid, 'IT' depname; create table emp as select 1 empid, 1 depid, 'John' empname union all select 2 empid, 1 depid, 'James' empname union all select 3 empid, 2 depid, 'Rob';
You can request it like this:
select dep.*, arr2crs(array( select row(emp.*)::emp from emp where emp.depid = dep.depid )) emps from dep
And process on the client side like this (Java)
public static List Rs2List(ResultSet rs) throws SQLException{ List result = new ArrayList(); ResultSetMetaData meta = rs.getMetaData(); while(rs.next()){ Map row = new HashMap(); for (int i = 1; i <= meta.getColumnCount(); i++){ Object o = rs.getObject(i); row.put( meta.getColumnName(i), (o instanceof ResultSet)?Rs2List((ResultSet)o):o); } result.add(row); } return result; }
Note that you must explicitly specify a string for a specific type. You can use CREATE TYPE to create the necessary types.
source share