Re: Returning NULL results?

From: Mario Splivalo <mario(dot)splivalo(at)mobart(dot)hr>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Returning NULL results?
Date: 2005-10-12 07:36:51
Message-ID: 1129102611.9305.12.camel@ekim
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)splivalo(at)mobart(dot)hr

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2005-10-12 10:34:32 Re: SEVEN cross joins?!?!?
Previous Message farhin 2005-10-12 06:53:00 Very Urgent Req for SQL-DBS In Chennai