Re: Prepared query ? - Mailing list pgsql-sql

From Richard Huxton
Subject Re: Prepared query ?
Date
Msg-id 425E852D.2020808@archonet.com
Whole thread Raw
In response to Prepared query ?  ("Dinesh Pandey" <dpandey@secf.com>)
Responses Re: Prepared query ?  ("Dinesh Pandey" <dpandey@secf.com>)
List pgsql-sql
Dinesh Pandey wrote:
> How can I
> 
>    1. Write a prepared query and
>    2. Set values at run time and
>    3. Execute that query for different values.

> LOOP
>      …..
>      …..
> sql := INSERT INTO MYTABLE VALUES(?, ?);

> --EXECUTE (sql);

You don't say what language you want to use, but assuming it's plpgsql, 
then for the simple case you can just do:

LOOP  INSERT INTO mytable VALUES (var1, var2);
END LOOP

Now, let's say you wanted to change mytable as well as the values, you'd use  sql := ''INSERT INTO '' ||
quote_ident(my_table_variable)|| '' 
 
VALUES  ...etc'';  EXECUTE sql;

Does that help?

--   Richard Huxton  Archonet Ltd


pgsql-sql by date:

Previous
From: Frank Bax
Date:
Subject: Re: Prepared query ?
Next
From: "Dinesh Pandey"
Date:
Subject: Re: dynamic 'INSERT' query?