Re: plpgsql dynamic queries and optional arguments - Mailing list pgsql-general

From Michael Fuhr
Subject Re: plpgsql dynamic queries and optional arguments
Date
Msg-id 20060816201538.GA73970@winnie.fuhr.org
Whole thread Raw
In response to Re: plpgsql dynamic queries and optional arguments  (Curtis Scheer <Curtis@DAYCOS.com>)
List 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

pgsql-general by date:

Previous
From: "Jasbinder Bali"
Date:
Subject: Re: [NOVICE] DB insert Error
Next
From: Tom Lane
Date:
Subject: Re: Segmentation Fault