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 E6ED78B9B1D for ; Mon, 31 Jan 2005 20:25:15 +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 52497-09 for ; Mon, 31 Jan 2005 20:25:08 +0000 (GMT) Received: from window.monsterlabs.com (window.monsterlabs.com [216.183.105.176]) by svr1.postgresql.org (Postfix) with SMTP id 3913B8B9B47 for ; Mon, 31 Jan 2005 20:25:08 +0000 (GMT) Received: (qmail 23288 invoked from network); 31 Jan 2005 20:25:07 -0000 Received: from w080.z064003242.bna-tn.dsl.cnc.net (HELO ?192.168.1.22?) (64.3.242.80) by 0 with SMTP; 31 Jan 2005 20:25:07 -0000 In-Reply-To: <6c21003b0501301141234c256b@mail.gmail.com> References: <6c21003b0501301141234c256b@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <9719e3dd9d6201fc40efcf3a15b82442@sitening.com> Content-Transfer-Encoding: 7bit Cc: pgsql-sql@postgresql.org From: Thomas F.O'Connell Subject: Re: plpgsql functions and NULLs Date: Mon, 31 Jan 2005 14:25:03 -0600 To: Don Drake X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.048 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/314 X-Sequence-Number: 20102 This sounds like a perfect candidate for a LEFT OUTER JOIN. See: http://www.postgresql.org/docs/7.4/static/queries-table- expressions.html#QUERIES-FROM Yours would looks something like: SELECT * FROM ... LEFT JOIN candidate AS c ON <...>.omcr_id = c.omcr_id AND ... -tfo -- Thomas F. O'Connell Co-Founder, Information Architect Sitening, LLC http://www.sitening.com/ 110 30th Avenue North, Suite 6 Nashville, TN 37203-6320 615-260-0005 On Jan 30, 2005, at 1:41 PM, Don Drake wrote: > OK, I have a function that finds records that changed in a set of > tables and attempts to insert them into a data warehouse. > > There's a large outer loop of candidate rows and I inspect them to see > if the values really changed before inserting. > > My problem is that when I look to see if the row exists in the > warehouse already, based on some IDs, it fails when an ID is NULL. > The ID is nullable, so that's not a problem. > > But I'm forced to write an IF statement looking for the potential NULL > and write 2 queries: > > IF omcr_id is null > select * from .... > WHERE omcr_id is NULL > AND ... > ELSE > select * from .... > WHERE omcr_id=candidate.omcr_id > AND .... > END IF; > > IF FOUND > ... > > Is there a way to do the lookup in one statement?? This could get ugly > quick. I'm using v7.4. > > Thanks. > > -Don > > -- > Donald Drake > President > Drake Consulting > http://www.drakeconsult.com/ > 312-560-1574 > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster