Re: Prepared query ?

From: "Dinesh Pandey" <dpandey(at)secf(dot)com>
To: "'Richard Huxton'" <dev(at)archonet(dot)com>
Cc: "'PostgreSQL'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Prepared query ?
Date: 2005-04-14 15:03:45
Message-ID: 20050414150625.0A51653603@svr1.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Yes you got my problem correctly. I want to change tablename and values at
run time.

Is there any other way instead using "||" (append) ? like prepared
statement.

PREPARE fooplan (int, text, bool, numeric) AS
INSERT INTO foo VALUES($1, $2, $3, $4);
EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);

Thanks
Dinesh Pandey

-----Original Message-----
From: Richard Huxton [mailto:dev(at)archonet(dot)com]
Sent: Thursday, April 14, 2005 8:29 PM
To: dpandey(at)secf(dot)com
Cc: 'PostgreSQL'
Subject: Re: [SQL] Prepared query ?

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Joseph Krogh 2005-04-14 16:38:14 row-attribute in EXPLAIN-output doesn't match count(*)
Previous Message Richard Huxton 2005-04-14 14:58:53 Re: Prepared query ?