Re: plpgsql dynamic queries and optional arguments

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Curtis Scheer <Curtis(at)DAYCOS(dot)com>
Cc: "Harvey, Allan AC" <HarveyA(at)OneSteel(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql dynamic queries and optional arguments
Date: 2006-08-16 20:15:39
Message-ID: 20060816201538.GA73970@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Aug 16, 2006 at 02:36:44PM -0500, Curtis Scheer wrote:
> Thanks for the reply I guess what I am actually looking for is an example of
> a dynamic SQL select statement similar to how a static sql select can select
> into a variable.

In 8.1 you can select a single row or columns of a single row with
INTO:

EXECUTE 'SELECT * FROM foo' INTO rec;

Earlier versions don't support INTO with EXECUTE but you can use a
loop to achieve the same effect:

FOR rec IN EXECUTE 'SELECT * FROM foo' LOOP
-- do stuff with rec
END LOOP;

Here are links to the relevant documentation:

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

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-08-16 20:21:19 Re: Segmentation Fault
Previous Message Jasbinder Bali 2006-08-16 20:12:45 Re: [NOVICE] DB insert Error