Re: Dynamic SQL - Mailing list pgsql-general

From Richard Huxton
Subject Re: Dynamic SQL
Date
Msg-id 40BD8E7E.9080200@archonet.com
Whole thread Raw
In response to Dynamic SQL  (Hadley Willan <hadley.willan@deeperdesign.co.nz>)
List pgsql-general
Hadley Willan wrote:
> Hi all,
>     I'd like to write a function that would allow me to verify some
> attributes on a table so that I could give a meaningful error message...
>

>      constructedSql = ''SELECT INTO recCheckObject * FROM '' ||
> tableName || '' WHERE '' || idColumn || '' = '' || objectId;
>      EXECUTE constructedSql;

> However, when you try and run this it's like the SELECT INTO <record> is
> failing to pickup the reference to the declared variable?

Sounds likely, and (without testing anything) I wouldn't expect it to work.

Indeed, checking the manuals: plpgsql / basic statements (37.6.4)
"The results from SELECT commands are discarded by EXECUTE, and SELECT
INTO is not currently supported within EXECUTE. There are two ways to
extract a result from a dynamically-created SELECT: one is to use the
FOR-IN-EXECUTE loop form described in Section 37.7.4, and the other is
to use a cursor with OPEN-FOR-EXECUTE, as described in Section 37.8.2."

There's your solution - you need to use the FOR rec IN ... looping
construct.

--
   Richard Huxton
   Archonet Ltd

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: after using pg_resetxlog, db lost
Next
From: Richard Huxton
Date:
Subject: Re: Providing the password to psql from a script