Re: EXECUTE with SELECT INTO variable, or alternatives

From: "Thomas F(dot) O'Connell" <tfo(at)sitening(dot)com>
To: mario(dot)splivalo(at)mobart(dot)hr
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: EXECUTE with SELECT INTO variable, or alternatives
Date: 2005-09-30 20:59:06
Message-ID: 1045972B-4D8E-4801-A958-76A1DB6FAD0B@sitening.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message solarsail 2005-09-30 22:47:48 query tables based on a query
Previous Message Jaime Casanova 2005-09-30 18:40:09 Re: combination of function to simple query makes query slow