X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AA2D953ECD for ; Thu, 14 Apr 2005 12:06:27 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 01504-04 for ; Thu, 14 Apr 2005 15:06:26 +0000 (GMT) Received: from secf.com (209-128-104-244.bayarea.net [209.128.104.244]) by svr1.postgresql.org (Postfix) with ESMTP id 0A51653603 for ; Thu, 14 Apr 2005 12:06:25 -0300 (ADT) Received: from dinesh by secf.com (MDaemon.PRO.v7.2.2.R) with ESMTP id md50000633599.msg for ; Thu, 14 Apr 2005 08:06:25 -0700 Reply-To: From: "Dinesh Pandey" To: "'Richard Huxton'" Cc: "'PostgreSQL'" Subject: Re: Prepared query ? Date: Thu, 14 Apr 2005 20:33:45 +0530 Organization: Second Foundation India P Ltd. MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 Thread-Index: AcVBAn/P5pzIDE78QCeDH9UWWFs5QAAABr9A In-Reply-To: <425E852D.2020808@archonet.com> X-Authenticated-Sender: dpandey@secf.com X-Spam-Processed: mail.secf.com, Thu, 14 Apr 2005 08:06:25 -0700 (not processed: message from valid local sender) X-MDRemoteIP: 220.227.32.11 X-Return-Path: dpandey@secf.com X-MDaemon-Deliver-To: pgsql-sql@postgresql.org X-MDAV-Processed: mail.secf.com, Thu, 14 Apr 2005 08:06:27 -0700 Message-Id: <20050414150625.0A51653603@svr1.postgresql.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.4 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, MSGID_FROM_MTA_ID X-Spam-Level: * X-Archive-Number: 200504/197 X-Sequence-Number: 21090 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@archonet.com] Sent: Thursday, April 14, 2005 8:29 PM To: dpandey@secf.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