Re: plpgsql functions and NULLs

From: Thomas F(dot)O'Connell <tfo(at)sitening(dot)com>
To: Don Drake <dondrake(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: plpgsql functions and NULLs
Date: 2005-01-31 20:25:03
Message-ID: 9719e3dd9d6201fc40efcf3a15b82442@sitening.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Don Drake 2005-01-31 21:06:34 Re: plpgsql functions and NULLs
Previous Message Ing. Jhon Carrillo 2005-01-31 18:59:12 error in function!!