From: Philipp Kraus <philipp.kraus@tu-clausthal.de>
To: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: sql function with empty row
Date: Wed, 16 May 2018 18:49:55 +0000
Message-ID: <72B01876-DA02-4306-9994-F7F1F12A7C29@tu-clausthal.de> (raw)
In-Reply-To: <fb79c263-b978-53eb-2a3d-c593e80980e0@aklaver.com>
References: <A5624A8E-1E70-49BC-9CBB-798AE6F8276F@tu-clausthal.de>
<fb79c263-b978-53eb-2a3d-c593e80980e0@aklaver.com>
> Am 16.05.2018 um 20:40 schrieb Adrian Klaver <adrian.klaver@aklaver.com>:
>
> 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.
>
> Well I was on the right track for the wrong reason. At any rate SETOF works:
>
> select * from cell_per where cell_per = 100;
> line_id | category | cell_per | ts_insert | ts_update | user_insert | user_update | plant_type | season | short_category
> ---------+----------+----------+-----------+-----------+-------------+-------------+------------+--------+----------------
> (0 rows)
>
> CREATE OR REPLACE FUNCTION cp(integer)
> RETURNS cell_per
> LANGUAGE 'sql'
> AS $BODY$
> select cell_per.* from cell_per where cell_per = $1;
> $BODY$;
>
>
> select * from cp(100);
> line_id | category | cell_per | ts_insert | ts_update | user_insert | user_update | plant_type | season | short_category
> ---------+----------+----------+-----------+-----------+-------------+-------------+------------+--------+----------------
> NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL
> (1 row)
>
>
> CREATE OR REPLACE FUNCTION cp(integer)
> RETURNS SETOF cell_per
> LANGUAGE 'sql'
> AS $BODY$
> select cell_per.* from cell_per where cell_per = $1;
> $BODY$;
>
>
> select * from cp(100);
> line_id | category | cell_per | ts_insert | ts_update | user_insert | user_update | plant_type | season | short_category
> ---------+----------+----------+-----------+-----------+-------------+-------------+------------+--------+----------------
> (0 rows)
I have tested it on my data and it works also, but that is a little bit confusing, because imho setof is >= 0 rows and
without setof it is [0,1]. On this I know there exist only one or no record, so I choose the solution without setof
Thanks for help
Phil
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: philipp.kraus@tu-clausthal.de, adrian.klaver@aklaver.com
Subject: Re: sql function with empty row
In-Reply-To: <72B01876-DA02-4306-9994-F7F1F12A7C29@tu-clausthal.de>
References: <A5624A8E-1E70-49BC-9CBB-798AE6F8276F@tu-clausthal.de> <fb79c263-b978-53eb-2a3d-c593e80980e0@aklaver.com> <72B01876-DA02-4306-9994-F7F1F12A7C29@tu-clausthal.de>
* 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