X-Original-To: pgsql-novice-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DDEEC3AC1C0 for ; Wed, 29 Dec 2004 18:59:21 +0000 (GMT) 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 09012-07 for ; Wed, 29 Dec 2004 18:59:14 +0000 (GMT) Received: from ll.mit.edu (LLMAIL.LL.MIT.EDU [129.55.12.40]) by svr1.postgresql.org (Postfix) with ESMTP id 652243AC187 for ; Wed, 29 Dec 2004 18:59:03 +0000 (GMT) Received: (from smtp@localhost) by ll.mit.edu (8.12.10/8.8.8) id iBTIx2f1006432; Wed, 29 Dec 2004 13:59:02 -0500 (EST) Received: from sty.llan.ll.mit.edu( ), claiming to be "sty.llan" via SMTP by llpost, id smtpdAAAy9a4ym; Wed Dec 29 13:58:58 2004 Date: Wed, 29 Dec 2004 13:58:58 -0500 From: george young To: sarlav kumar Cc: pgsql-novice@postgresql.org Subject: Re: [PERFORM] INSERT question Message-Id: <20041229135858.71f8c3cf.gry@ll.mit.edu> In-Reply-To: <20041213162839.60931.qmail@web51303.mail.yahoo.com> References: <20041213162839.60931.qmail@web51303.mail.yahoo.com> Reply-To: gry@ll.mit.edu Organization: MIT Lincoln Laboratory X-Mailer: Sylpheed version 0.9.10 (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200412/271 X-Sequence-Number: 11848 On Mon, 13 Dec 2004 08:28:39 -0800 (PST) sarlav kumar threw this fish to the penguins: > Hi All, > > I have a question regarding multiple inserts. > The following function inserts for each country found in country table, values into merchant_buyer_country. > > ----------------------------------------------------------------------------------------------------------------------------------------- > CSQLStatement st( sql ); > CSQLStatement st1( sql ); > SQLINTEGER rows; > long num_codes = 0; > rows = st.Select( "SELECT * FROM merchant_buyer_country where merchant_id = %lu ",merchant_id ); > if ( rows ) > return 0; > char code[4]; > rows = st.Select( "SELECT code FROM country WHERE send IS NOT NULL OR receive IS NOT NULL" ); > SQLBindCol( st.hstmt, 1, SQL_C_CHAR, code, sizeof(code), 0 ); > long i; > for (i = 0; i < rows; i++ ) > { > st.Fetch(); > st1.Command("INSERT INTO merchant_buyer_country (merchant_id,country,enabled,group_id) VALUES(%lu ,'%s', true, %lu )", merchant_id, > code,group_id); > } > st.CloseCursor(); > st1.CloseCursor(); > return 1; > ----------------------------------------------------------------------------------------------------------------------------------------- > > On looking at the log file, I saw separate inserts being performed, and each insert takes about 1 second. > > insert into merchant_buyer_country (merchant_id,country,enabled,group_id) values(1203,'IN','true',1); > insert into merchant_buyer_country merchant_id,country,enabled,group_id) values(1203,'US','true',1); > insert into merchant_buyer_country merchant_id,country,enabled,group_id) values (1203,'AR','true',1); > insert into merchant_buyer_country (merchant_id,country,enabled,group_id) values(1203,'AZ','true',1); > insert into merchant_buyer_country merchant_id,country,enabled,group_id) values (1203,'BG','true',1); > insert into merchant_buyer_country merchant_id,country,enabled,group_id) values(1203,'SP','true',1); > ..... > > > > > > > There are more than 100 countries and this takes a lot of time for the inserts to complete. > Is there a way to write the INSERT as follows? > > INSERT into merchant_buyer_country (merchant_id,country,enabled,group_id) values (1203, > (SELECT code FROM country WHERE send IS NOT NULL OR receive IS NOT NULL), 'true',1); > > I tried this, but I get the following problem: > ERROR: More than one tuple returned by a subselect used as an expression. > > I know there is a way to this, but I am not sure where I am going wrong. Can someone please help me figure this out. Try: insert into merchant_buyer_country select 1203,code,true,1 from country where send is not null or receive is not null; -- George Young -- "Are the gods not just?" "Oh no, child. What would become of us if they were?" (CSL)