pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas F. O'Connell <tfo@sitening.com>
To: mario.splivalo@mobart.hr
Cc: pgsql-sql@postgresql.org
Subject: Re: EXECUTE with SELECT INTO variable, or alternatives
Date: Fri, 30 Sep 2005 15:59:06 -0500
Message-ID: <1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.com> (raw)
In-Reply-To: <1128017805.9061.5.camel@localhost.localdomain>
References: <1128017805.9061.5.camel@localhost.localdomain>

Per the docs:

"The results from SELECT commands are discarded by EXECUTE, and  
SELECT INTO is not currently supported within EXECUTE. So there is no  
way to extract a result from a dynamically-created SELECT using the  
plain EXECUTE command. There are two other ways to do it, however:  
one is to use the FOR-IN-EXECUTE loop form described in Section  
35.7.4, and the other is to use a cursor with OPEN-FOR-EXECUTE, as  
described in Section 35.8.2."

http://www.postgresql.org/docs/8.0/static/plpgsql- 
statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

So you've already hit upon one of your options.

--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC

Strategic Open Source: Open Your i™

http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-469-5150
615-469-5151 (fax)

On Sep 29, 2005, at 1:16 PM, Mario Splivalo wrote:

> I can assign a value to a variable in several ways:
>
> myVar := (SELECT col FROM table WHERE somecondition...)
> myVar := col FROM table WHERE somecondtition...
> SELECT col INTO myVar FROM table WHERE somecondition
>
> How do I do any of the above using EXECUTE? I need to be able to  
> assign
> the value to a variable, a value returned by a querry on a temporary
> table.
>
> So far I have found workaround like this:
>
> myRec record;
>
> FOR rec IN EXECUTE ''SELECT col FROM table WHERE somecondition''
> LOOP
>     myVar := rec
> END LOOP
>
> Obviously, the above SELECT query returns only one row. Still, if is
> realy inconvinient to have FOR...LOOP...END LOOP construct for  
> assigning
> the value to a variable 'read' from the temporary table.
>
>     Mario



view thread (3+ messages)  latest in thread

Message-ID: <1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.com>
Permalink:  ../1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.com/
Also on:    postgresql.org/message-id/1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.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: tfo@sitening.com, mario.splivalo@mobart.hr
  Subject: Re: EXECUTE with SELECT INTO variable, or alternatives
  In-Reply-To: <1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.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