Re: dynamic 'INSERT' query? - Mailing list pgsql-sql

From Sean Davis
Subject Re: dynamic 'INSERT' query?
Date
Msg-id e9917654069296414165126cade4505c@mail.nih.gov
Whole thread Raw
In response to dynamic 'INSERT' query?  ("Dinesh Pandey" <dpandey@secf.com>)
List pgsql-sql
On Apr 14, 2005, at 7:37 AM, Dinesh Pandey wrote:

> How can we set A1, A2 values in dynamic 'INSERT’  query?
>
>  
>
> DECLARE
>
>  _record        RECORD;
>
>  _sql             VARCHAR(2000);
>
>  
>
> FOR _record IN SELECT A1, A2 FROM A
>
> LOOP
>
>  
>
> _sql := 'INSERT INTO B VALUES (:A1, :A2)’;
>
>  
>
> EXECUTE (_sql);
>
>  
>
> END LOOP;
>
>  
>
> =================================================================
>
> I can do this as (but I need another way instead of using || operator).
>
>  
>
> _sql := 'INSERT INTO B VALUES (‘ || _record.A1 || ’,’ || _record.A2 ||
> ’)’;
>

Dinesh,

I think what you are showing here IS the way to build up a dynamic sql
statement.  I'm not sure that you can write a prepared statement within
the body of a function, which would then look more like what you are
suggesting you want to do--perhaps others on the list can enlighten us
about that.  In any case, why won't using the || operator work for you?

Sean



pgsql-sql by date:

Previous
From: Andreas Joseph Krogh
Date:
Subject: Re: Very low performance on table with only 298 rows
Next
From: "Dinesh Pandey"
Date:
Subject: Re: dynamic 'INSERT' query?