Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all rows from refcursor returned by PL/pgSQL function?

I have a function some_func() that returns refcursor:

CREATE OR REPLACE FUNCTION some_func() RETURNS refcursor AS (...)

I want to call this function from console and display the result set from the cursor returned by it. In Oracle I would write:

SELECT * FROM TABLE(some_func());

What is the equivalent of that construction on PosgreSQL?

like image 216
Danubian Sailor Avatar asked Oct 08 '22 11:10

Danubian Sailor


1 Answers

A refcursor is referred to by its name, either auto-generated or chosen by you. This page of the doc gives an example for each.

To fetch results from a refcursor you must have the cursor's name. In the case of generated names that'll be something like <unnamed portal 1>". You can then:

FETCH ALL FROM "<unnamed portal 1>";

The cursor name is returned from the function as the refcursor result, so you can get it from there.

like image 149
Daniel Vérité Avatar answered Oct 18 '22 09:10

Daniel Vérité