From dpandey@secf.com Thu Apr 14 11:41:40 2005 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 8F5E153734 for ; Thu, 14 Apr 2005 08:41:40 -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 12322-05 for ; Thu, 14 Apr 2005 11:41:34 +0000 (GMT) Received: from secf.com (209-128-104-244.bayarea.net [209.128.104.244]) by svr1.postgresql.org (Postfix) with ESMTP id C840B53726 for ; Thu, 14 Apr 2005 08:41:31 -0300 (ADT) Received: from dinesh by secf.com (MDaemon.PRO.v7.2.2.R) with ESMTP id md50000632816.msg for ; Thu, 14 Apr 2005 04:40:32 -0700 Reply-To: From: "Dinesh Pandey" To: "'PostgreSQL'" Subject: dynamic 'INSERT' query? Date: Thu, 14 Apr 2005 17:07:54 +0530 Organization: Second Foundation India P Ltd. MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0025_01C54114.815D64A0" X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 Thread-Index: AcVA5mVy32wnVP7MThavkPaeQwA55w== X-Authenticated-Sender: dpandey@secf.com X-Spam-Processed: mail.secf.com, Thu, 14 Apr 2005 04:40:32 -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 04:40:32 -0700 Message-Id: <20050414114131.C840B53726@svr1.postgresql.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.502 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, HTML_MESSAGE, MSGID_FROM_MTA_ID, UPPERCASE_25_50 X-Spam-Level: * X-Archive-Number: 200504/187 X-Sequence-Number: 21080 This is a multi-part message in MIME format. ------=_NextPart_000_0025_01C54114.815D64A0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 || ')'; ================================================================= Regards Dinesh Pandey ---------------------------------------------------------------------------- ------ Dinesh Pandey Sr. Software Engineer ------=_NextPart_000_0025_01C54114.815D64A0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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<= /p>

 

_sql :=3D 'INSERT INTO B = VALUES (:A1, = :A2)’;

 

EXECUTE = (_sql);

 

END LOOP;

 

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

I can do this as (but I need another way instead of = using || operator).

 

_sql :=3D 'INSERT INTO B = VALUES (‘ || _record.A1 || ’,’ || _record.A2 || = ’)’;

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

 

Regards
Dinesh Pandey



-----------------------------------= -----------------------------------------------
<= /font>
Dinesh = Pandey 
Sr. Software Engineer

 

------=_NextPart_000_0025_01C54114.815D64A0-- From sdavis2@mail.nih.gov Thu Apr 14 11:59:13 2005 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 2453B5366A for ; Thu, 14 Apr 2005 08:59:13 -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 18971-04 for ; Thu, 14 Apr 2005 11:59:04 +0000 (GMT) Received: from mx5.net.nih.gov (mailfwd.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id 35057536F6 for ; Thu, 14 Apr 2005 08:59:03 -0300 (ADT) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j3EBx5aq028133 for ; Thu, 14 Apr 2005 07:59:05 -0400 Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j3EBx5Kd028130; Thu, 14 Apr 2005 07:59:05 -0400 In-Reply-To: <20050414114131.C840B53726@svr1.postgresql.org> References: <20050414114131.C840B53726@svr1.postgresql.org> Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=WINDOWS-1252; format=flowed Message-Id: Content-Transfer-Encoding: quoted-printable Cc: "'PostgreSQL'" From: Sean Davis Subject: Re: dynamic 'INSERT' query? Date: Thu, 14 Apr 2005 07:59:05 -0400 To: X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/189 X-Sequence-Number: 21082 On Apr 14, 2005, at 7:37 AM, Dinesh Pandey wrote: > How can we set A1, A2 values in dynamic 'INSERT=92 =A0query? > > =A0 > > DECLARE > > =A0_record =A0=A0=A0=A0=A0=A0 RECORD; > > =A0_sql=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 VARCHAR(2000); > > =A0 > > FOR _record IN SELECT A1, A2 FROM A > > LOOP > > =A0 > > _sql :=3D 'INSERT INTO B VALUES (:A1, :A2)=92; > > =A0 > > EXECUTE (_sql); > > =A0 > > END LOOP; > > =A0 > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > I can do this as (but I need another way instead of using || = operator). > > =A0 > > _sql :=3D 'INSERT INTO B VALUES (=91 || _record.A1 || =92,=92 || = _record.A2 ||=20 > =92)=92; > Dinesh, I think what you are showing here IS the way to build up a dynamic sql=20= statement. I'm not sure that you can write a prepared statement within=20= the body of a function, which would then look more like what you are=20 suggesting you want to do--perhaps others on the list can enlighten us=20= about that. In any case, why won't using the || operator work for you? Sean From dpandey@secf.com Thu Apr 14 12:06:43 2005 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 3B80A53689 for ; Thu, 14 Apr 2005 09:06:43 -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 22309-06 for ; Thu, 14 Apr 2005 12:06:36 +0000 (GMT) Received: from secf.com (209-128-104-244.bayarea.net [209.128.104.244]) by svr1.postgresql.org (Postfix) with ESMTP id D513853646 for ; Thu, 14 Apr 2005 09:06:34 -0300 (ADT) Received: from dinesh by secf.com (MDaemon.PRO.v7.2.2.R) with ESMTP id md50000632922.msg for ; Thu, 14 Apr 2005 05:05:54 -0700 Reply-To: From: "Dinesh Pandey" To: "'Juris Zeltins'" Cc: "'PostgreSQL'" Subject: Re: dynamic 'INSERT' query? Date: Thu, 14 Apr 2005 17:33:13 +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: AcVA6Ee77Kn9jtcBRE+utxpzPwRjuAAAYp0g In-Reply-To: <425E57DD.6060203@gmail.com> X-Authenticated-Sender: dpandey@secf.com X-Spam-Processed: mail.secf.com, Thu, 14 Apr 2005 05:05:54 -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 05:05:55 -0700 Message-Id: <20050414120634.D513853646@svr1.postgresql.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.503 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, MSGID_FROM_MTA_ID, UPPERCASE_25_50 X-Spam-Level: * X-Archive-Number: 200504/190 X-Sequence-Number: 21083 FOR _record IN SELECT A1, A2 FROM A LOOP --[Dinesh Pandey] -- Sorry I didn't get this part. _sql := "INSERT INTO B VALUES ( '*" || _record.A1 || "'** , '" || _record.A2 || "' ***)"; EXECUTE (_sql); END LOOP; 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 || > ')'; > > ================================================================= > > Regards > Dinesh Pandey > > > *--------------------------------------------------------------------------- -------* > *Dinesh Pandey* > Sr. Software Engineer > From tgl@sss.pgh.pa.us Thu Apr 14 14:53:17 2005 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 F385453D83 for ; Thu, 14 Apr 2005 11:53:17 -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 95778-01 for ; Thu, 14 Apr 2005 14:53:14 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 2D04653710 for ; Thu, 14 Apr 2005 11:53:10 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j3EErEYC000565; Thu, 14 Apr 2005 10:53:15 -0400 (EDT) To: dpandey@secf.com Cc: "'PostgreSQL'" Subject: Re: dynamic 'INSERT' query? In-reply-to: <20050414114131.C840B53726@svr1.postgresql.org> References: <20050414114131.C840B53726@svr1.postgresql.org> Comments: In-reply-to "Dinesh Pandey" message dated "Thu, 14 Apr 2005 17:07:54 +0530" Date: Thu, 14 Apr 2005 10:53:14 -0400 Message-ID: <564.1113490394@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/192 X-Sequence-Number: 21085 "Dinesh Pandey" writes: > FOR _record IN SELECT A1, A2 FROM A > LOOP > _sql := 'INSERT INTO B VALUES (:A1, :A2)'; > EXECUTE (_sql); Why do you need a dynamic query here at all? You could just do FOR _record IN SELECT A1, A2 FROM A LOOP INSERT INTO B VALUES (_record.A1, _record.A2); This would be much faster as well as simpler to code. As far as I can see offhand, you only need EXECUTE when you want to change table and/or field names from one execution to the next of a particular query. If you have such a problem, you are omitting the important details ... regards, tom lane From dpandey@secf.com Thu Apr 14 15:01:07 2005 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 1DA1953724 for ; Thu, 14 Apr 2005 12:01:07 -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 97763-08 for ; Thu, 14 Apr 2005 15:01:05 +0000 (GMT) Received: from secf.com (209-128-104-244.bayarea.net [209.128.104.244]) by svr1.postgresql.org (Postfix) with ESMTP id 457BB53F8C for ; Thu, 14 Apr 2005 12:01:04 -0300 (ADT) Received: from dinesh by secf.com (MDaemon.PRO.v7.2.2.R) with ESMTP id md50000633583.msg for ; Thu, 14 Apr 2005 08:00:24 -0700 Reply-To: From: "Dinesh Pandey" To: "'Tom Lane'" Cc: "'PostgreSQL'" Subject: Re: dynamic 'INSERT' query? Date: Thu, 14 Apr 2005 20:27:46 +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: AcVBAbbxGsSPlARtRC2+jib20x9jzQAAAYjg In-Reply-To: <564.1113490394@sss.pgh.pa.us> X-Authenticated-Sender: dpandey@secf.com X-Spam-Processed: mail.secf.com, Thu, 14 Apr 2005 08:00:24 -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:00:27 -0700 Message-Id: <20050414150104.457BB53F8C@svr1.postgresql.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.508 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, MSGID_FROM_MTA_ID, UPPERCASE_25_50 X-Spam-Level: * X-Archive-Number: 200504/195 X-Sequence-Number: 21088 Yes here the TABLE NAME is also coming at runtime. Like In Oracle I have done in this way: ------------------------------------------- LOOP v_sql := 'INSERT INTO ' || mytable || ' VALUES(:in_sentryid_id, :in_node_id)'; EXECUTE IMMEDIATE v_sql USING v_sentryid, v_nodeid; END LOOP; How to convert this into Postgres????? Thanks Dinesh Pandey