pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Philipp Kraus <philipp.kraus@tu-clausthal.de>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: sql function with empty row
Date: Wed, 16 May 2018 11:21:46 -0700
Message-ID: <e57b0979-e0e0-32a7-2c81-7455b6910cc8@aklaver.com> (raw)
In-Reply-To: <A5624A8E-1E70-49BC-9CBB-798AE6F8276F@tu-clausthal.de>
References: <A5624A8E-1E70-49BC-9CBB-798AE6F8276F@tu-clausthal.de>

On 05/16/2018 11:07 AM, Philipp Kraus wrote:
> Hello,
> 
> I have defined a SQL function
> 
> CREATE OR REPLACE FUNCTION substancetrivialname(text)
>      RETURNS substance
>      LANGUAGE 'sql'
>      COST 100
>      VOLATILE
> AS $BODY$
> select s.* from substancetrivialname n
>      join substance s on s.id = n.idsubstance
> 	where lower(btrim(n.name)) = lower(btrim($1));
> $BODY$;
> 
> substance and substancetrivialname have got a 1-to-N relationship (for each substance can exist multiple trivial names).
> If I call the function with a non-existing trivial name it returns a single row with all fields are set to NULL.

Since there can be many trivial names per substance shouldn't you be 
using SETOF?:

https://www.postgresql.org/docs/10/static/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-RETURNING-SET

> 
> If I run the join query directly it returns an empty record set on a non-existing trivial name.
> I expected equal behavior on my function, so my question is, how can I fix this?
> 
> Thanks
> 
> Phil
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (9+ messages)  latest in thread

Message-ID: <e57b0979-e0e0-32a7-2c81-7455b6910cc8@aklaver.com>
Permalink:  ../e57b0979-e0e0-32a7-2c81-7455b6910cc8@aklaver.com/
Also on:    postgresql.org/message-id/e57b0979-e0e0-32a7-2c81-7455b6910cc8@aklaver.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, philipp.kraus@tu-clausthal.de
  Subject: Re: sql function with empty row
  In-Reply-To: <e57b0979-e0e0-32a7-2c81-7455b6910cc8@aklaver.com>
  References: <A5624A8E-1E70-49BC-9CBB-798AE6F8276F@tu-clausthal.de> <e57b0979-e0e0-32a7-2c81-7455b6910cc8@aklaver.com>

* 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