Обсуждение: EXECUTE with SELECT INTO variable, or alternatives

Поиск
Список
Период
Сортировка

EXECUTE with SELECT INTO variable, or alternatives

От
Mario Splivalo
Дата:
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''
LOOPmyVar := 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



Re: EXECUTE with SELECT INTO variable, or alternatives

От
"Thomas F. O'Connell"
Дата:
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


Re: EXECUTE with SELECT INTO variable, or alternatives

От
Michael Fuhr
Дата:
On Fri, Sep 30, 2005 at 03:59:06PM -0500, Thomas F. O'Connell wrote:
> 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."

8.1 will have EXECUTE ... INTO.

http://developer.postgresql.org/docs/postgres/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

-- 
Michael Fuhr