pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: te <tejal_1910@yahoo.co.in>
To: pgsql-bugs@postgresql.org
Subject: Re: how to proccess record returning null
Date: Mon, 17 Sep 2012 13:24:10 -0700 (PDT)
Message-ID: <1347913450237-5724361.post@n5.nabble.com> (raw)
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>

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.




view thread (5+ messages)

Message-ID: <1347913450237-5724361.post@n5.nabble.com>
Permalink:  ../1347913450237-5724361.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1347913450237-5724361.post@n5.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tejal_1910@yahoo.co.in
  Subject: Re: how to proccess record returning null
  In-Reply-To: <1347913450237-5724361.post@n5.nabble.com>
  References: <1347563825521-5723932.post@n5.nabble.com> <505265EA.7040901@hogranch.com> <1347636697274-5724061.post@n5.nabble.com> <5053A806.4060700@hogranch.com> <1347913450237-5724361.post@n5.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox