Re: EXECUTE with SELECT INTO variable, or alternatives - Mailing list pgsql-sql

From Thomas F. O'Connell
Subject Re: EXECUTE with SELECT INTO variable, or alternatives
Date
Msg-id 1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.com
Whole thread Raw
In response to EXECUTE with SELECT INTO variable, or alternatives  (Mario Splivalo <mario.splivalo@mobart.hr>)
Responses Re: EXECUTE with SELECT INTO variable, or alternatives
List pgsql-sql
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


pgsql-sql by date:

Previous
From: Jaime Casanova
Date:
Subject: Re: combination of function to simple query makes query slow
Next
From: Michael Fuhr
Date:
Subject: Re: EXECUTE with SELECT INTO variable, or alternatives