Re: Returning NULL results? - Mailing list pgsql-sql

From Mario Splivalo
Subject Re: Returning NULL results?
Date
Msg-id 1129102611.9305.12.camel@ekim
Whole thread Raw
In response to Re: Returning NULL results?  (Richard Huxton <dev@archonet.com>)
List pgsql-sql
On Tue, 2005-10-11 at 15:39 +0100, Richard Huxton wrote:
> Mario Splivalo wrote:
> > 
> > Of course, if it returns no rows, I'm presented with an error, saying:
> > 
> > ERROR:  record "rec" has no field "_qv"
> > 
> > This is logical. My question would be is there a way around this
> > withouth first doing SELECT COUNT(*) FROM tmbBids WHERE ..., and then if
> > COUNT(*) is zero THEN bidCount := NULL ELSE if COUNT(*) is not zero then
> > do SELECT from the begining?
> 
> Could you check the FOUND variable? As in IF NOT FOUND THEN RETURN NULL
> 

That actually works, but it's a bit of fuss to use. I need to have it
inside the FOR loop, then exit the loop if FOUND is NOT TRUE, wich makes
the code even harder to read/mantain. Do you think that anytime soon
would we be able to do SELECTs from the temporary tables withouth
EXECing those selects? And, some qualifier for the variables (@ in
T-SQL, $ in PHP, or something similair) would make the code even more
readable. Just my tought.

Btw, on postgres 7.4 you can do SELECT something FROM ...., and if query
returns no rows, the FOR loop is skipped. So, it's safe to do something
like:

someVar:=NULL;
_SQL:='SELECT someVar FROM someTable WHERE someCondition...'
FOR rec IN EXECUTE _SQL LOOP someVar:=rec.someVar; END LOOP;

Now, if select returns no rows, the LOOP/END LOOP is skippend, and
someVar is se to be null before FOR. 
Mario
-- 
Mario Splivalo
Mob-Art
mario.splivalo@mobart.hr

"I can do it quick, I can do it cheap, I can do it well. Pick any two."




pgsql-sql by date:

Previous
From: "Greg Patnude"
Date:
Subject: Re: SEVEN cross joins?!?!?
Next
From: Richard Huxton
Date:
Subject: Re: SEVEN cross joins?!?!?