pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mario Splivalo <mario.splivalo@mobart.hr>
To: Richard Huxton <dev@archonet.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Returning NULL results?
Date: Wed, 12 Oct 2005 09:36:51 +0200
Message-ID: <1129102611.9305.12.camel@ekim> (raw)
In-Reply-To: <434BCE99.1040608@archonet.com>
References: <1129040845.7911.9.camel@ekim>
	<434BCE99.1040608@archonet.com>

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."





view thread (3+ messages)

Message-ID: <1129102611.9305.12.camel@ekim>
Permalink:  ../1129102611.9305.12.camel@ekim/
Also on:    postgresql.org/message-id/1129102611.9305.12.camel@ekim

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mario.splivalo@mobart.hr, dev@archonet.com
  Subject: Re: Returning NULL results?
  In-Reply-To: <1129102611.9305.12.camel@ekim>

* 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