Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TDhrd-0003w3-Jd for pgsql-bugs@postgresql.org; Mon, 17 Sep 2012 20:24:17 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TDhrZ-0001Ba-Hf for pgsql-bugs@postgresql.org; Mon, 17 Sep 2012 20:24:16 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1TDhrW-0003DI-7y for pgsql-bugs@postgresql.org; Mon, 17 Sep 2012 13:24:10 -0700 Date: Mon, 17 Sep 2012 13:24:10 -0700 (PDT) From: te To: pgsql-bugs@postgresql.org Message-ID: <1347913450237-5724361.post@n5.nabble.com> In-Reply-To: <5053A806.4060700@hogranch.com> References: <1347563825521-5723932.post@n5.nabble.com> <505265EA.7040901@hogranch.com> <1347636697274-5724061.post@n5.nabble.com> <5053A806.4060700@hogranch.com> Subject: Re: how to proccess record returning null MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 0.4 (/) X-Archive-Number: 201209/177 X-Sequence-Number: 34172 What I want to do is, CREATE OR REPLACE FUNCTION clean() RETURNS void AS $$ DECLARE r record; BEGIN FOR r in select distinct(id) from temp loop If r.id is null or r.id ='' Then Insert into table_1 select * from temp; else Insert into table_1 select * from temp where sequence_number=r.id; end if; end loop; END; $$ LANGUAGE plpgsql; If 'id' is null then I want to insert all the records from table 'temp' to 'table_1' But if 'id' is not null I want to insert the data corresponding to those id's from table 'temp' to 'table_1'. Since id's are null or empty sting, control is not going inside the loop and as a result no data is inserted from table 'temp' to table 'table_1'. Currently I have come up with following workaround where null value is checked separately out of loop. CREATE OR REPLACE FUNCTION clean() RETURNS void AS $$ DECLARE r record; BEGIN if (select distinct(id) from temp ) is null then Insert into table_1 select * from temp; end if; FOR r in select distinct(id) from temp loop Insert into table_1 select * from temp where sequence_number=r.id; end loop; END; $$ LANGUAGE plpgsql; But I think this is inefficient way of coding. Do you have any better alternative to this ? I hope I am clear. -- View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-proccess-record-returning-null-tp5723932p5724361.html Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.