From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Philipp Kraus <philipp.kraus@tu-clausthal.de>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: cursor empty
Date: Tue, 8 May 2018 06:36:08 -0700
Message-ID: <bbdbd48c-c3be-d8ca-0c3d-39231f4ecdf0@aklaver.com> (raw)
In-Reply-To: <C65B4D42-269A-4881-9F37-22B81FE8EE28@tu-clausthal.de>
References: <C65B4D42-269A-4881-9F37-22B81FE8EE28@tu-clausthal.de>
On 05/08/2018 05:39 AM, Philipp Kraus wrote:
> Hello,
>
> I have got a function with this definition:
>
> CREATE OR REPLACE FUNCTION vectorize(refcursor)
> RETURNS SETOF refcursor
> LANGUAGE 'plpgsql'
> COST 100
> STABLE
> ROWS 1000
> AS $BODY$
>
> begin
> perform pivottable(
> '_vector',
> 'select * from viewdata',
> array['ideate', 'name', 'description', 'latitude', 'longitude'],
> array['parametername'],
> array['parametervalue::text', 'parametervaluetext']
> );
> open $1 scroll for select * from _vector;
> return next $1;
> end
>
> $BODY$;
>
> The perform call creates a dynamic column pivot table, if I run manually
>
> select pivottable(
> '_vector',
> 'select * from viewdata',
> array['ideate', 'name', 'description', 'latitude', 'longitude'],
> array['parametername'],
> array['parametervalue::text', 'parametervaluetext']
> );
> select * from _vector;
>
> I get all the data in the output, so everything is fine.
>
> My goal is now to encapsulate the two lines into a function, so I define
> a stable function and based on the dynamic column set a cursor. I get in
> pgadmin the column names back, but the rows are empty if I run:
>
> select * from vectorize('myvec');
> fetch all from myvec;
>
> Can you explain me, which part is wrong?
I am going to say:
perform pivottable( ...
https://www.postgresql.org/docs/10/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-NORESULT
"Sometimes it is useful to evaluate an expression or SELECT query but
discard the result, for example when calling a function that has
side-effects but no useful result value. To do this in PL/pgSQL, use the
PERFORM statement:
PERFORM query;
This executes query and discards the result. ..."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Thanks
>
> Phil
>
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, philipp.kraus@tu-clausthal.de
Subject: Re: cursor empty
In-Reply-To: <bbdbd48c-c3be-d8ca-0c3d-39231f4ecdf0@aklaver.com>
References: <C65B4D42-269A-4881-9F37-22B81FE8EE28@tu-clausthal.de> <bbdbd48c-c3be-d8ca-0c3d-39231f4ecdf0@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox