pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Karl Grossner <karl.geog@gmail.com>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: returning values from dynamic SQL to a variable
Date: Tue, 15 Jan 2013 16:40:32 -0800
Message-ID: <50F5F700.30202@gmail.com> (raw)
In-Reply-To: <CAFj8pRBqxje9RNmnJribA82bvX4DsqSpL=syQyUw5A8+DCm+SQ@mail.gmail.com>
References: <CC711732.32BC%jsharrett@tidemark.net>
	<CAL_0b1vmtwqqjK4B2o7p9n3CWnV0SqGw6qms2zjm0oRfak=Myg@mail.gmail.com>
	<1358269730873-5740324.post@n5.nabble.com>
	<CAFj8pRBqxje9RNmnJribA82bvX4DsqSpL=syQyUw5A8+DCm+SQ@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

Pavel -

RETURN QUERY EXECUTE worked, many thanks for responding so quickly. The 
docs show no relevant examples, so for anyone else, something like this

create or replace function getRowsE(
     OUT element character(1), OUT name character varying(100), OUT sum 
numeric
) returns setof record as $BODY$
declare
  r record;
  i integer;
  usesql text;
begin
  for r in select * from mytable where id is not null order by id loop
   i := r.graphid;
   usesql := 'bunch of sql where ' || i || 'something or other, 
producing element, name, sum';
   RETURN QUERY EXECUTE usesql;
  end loop;
  return;
end;
$BODY$ language 'plpgsql';


On 1/15/2013 10:23 AM, Pavel Stehule wrote:
> Hello
>
> you can use RETURN QUERY EXECUTE statement
>
> http://www.postgresql.org/docs/9.1/interactive/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RE...
>
> Regards
>
> Pavel Stehule
>
> 2013/1/15 kgeographer <karl.geog@gmail.com>:
>> I have a related problem and tried the PERFORM...EXECUTE pattern suggested
>> but no matter where I put PERFORM I get 'function not found' errors.
>>
>> I want to loop through id values returned by a query and execute another
>> with each i as a parameter. Each subquery will return 6-8 rows. This is a
>> simplified example, in the real app the subquery is doing some aggregation
>> work.
>>
>> Tried many many things including this pattern below and read everything I
>> could find, but no go. Any help appreciated.
>>
>> ++++++++++++++++
>> create or replace function getRowsA() returns setof record as $$
>> declare
>>   r record;
>>   loopy record;
>>   i integer;
>>   sql text;
>> begin
>>   for r in select * from cities loop
>>    i := r.id;
>>    sql := 'select city,topic,weight from v_doctopic where city = ' || i;
>>    EXECUTE sql;
>>    return next loopy;
>>   end loop;
>>   return;
>> end;
>> $$ language 'plpgsql';
>>
>> select * from getRowsA() AS foo(city int, topic int, weight numeric)
>>
>>
>>
>> -----
>> karlg
>> --
>> View this message in context: http://postgresql.1045698.n5.nabble.com/returning-values-from-dynamic-SQL-to-a-variable-tp5723322p57...
>> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>>
>>
>> --
>> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-sql

view thread (5+ messages)

Message-ID: <50F5F700.30202@gmail.com>
Permalink:  ../50F5F700.30202@gmail.com/
Also on:    postgresql.org/message-id/50F5F700.30202@gmail.com

 · 

reply

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: karl.geog@gmail.com, pavel.stehule@gmail.com
  Subject: Re: returning values from dynamic SQL to a variable
  In-Reply-To: <50F5F700.30202@gmail.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