Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJ1V2-0004qx-NY for pgsql-general@arkaria.postgresql.org; Wed, 16 May 2018 18:50:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fJ1Uy-0007c2-UQ for pgsql-general@arkaria.postgresql.org; Wed, 16 May 2018 18:50:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJ1Uy-0007br-Ml for pgsql-general@lists.postgresql.org; Wed, 16 May 2018 18:50:04 +0000 Received: from mailrelay2.rz.tu-clausthal.de ([2001:638:605:1027:1::9]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJ1Us-0001wf-08 for pgsql-general@postgresql.org; Wed, 16 May 2018 18:50:04 +0000 Received: from poseidon.rz.tu-clausthal.de (poseidon.rz.tu-clausthal.de [IPv6:2001:638:605:1027:1::7]) by mailrelay2.rz.tu-clausthal.de (Postfix) with ESMTPS id 24A998250A; Wed, 16 May 2018 20:49:56 +0200 (CEST) Received: from poseidon.rz.tu-clausthal.de (localhost [127.0.0.1]) by localhost (Postfix) with SMTP id 21ACA65B97; Wed, 16 May 2018 20:49:56 +0200 (CEST) Received: from exchange.tu-clausthal.de (smtp.alumni.tu-clausthal.de [IPv6:2001:638:605:1027:1::21]) by poseidon.rz.tu-clausthal.de (Postfix) with ESMTPS id E994565B72; Wed, 16 May 2018 20:49:55 +0200 (CEST) Received: from exchange3.rz.tu-clausthal.de (139.174.3.2) by exchange4.rz.tu-clausthal.de (139.174.3.3) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1261.35; Wed, 16 May 2018 20:49:55 +0200 Received: from exchange3.rz.tu-clausthal.de ([::1]) by exchange3.rz.tu-clausthal.de ([fe80::99e7:25da:17c:14dc%5]) with mapi id 15.01.1261.035; Wed, 16 May 2018 20:49:55 +0200 From: Philipp Kraus To: Adrian Klaver CC: "pgsql-general@postgresql.org" Subject: Re: sql function with empty row Thread-Topic: sql function with empty row Thread-Index: AQHT7UDMTGNVjO1/Lk2gCqCVqU4QXaQyjpSAgAACv4A= Date: Wed, 16 May 2018 18:49:55 +0000 Message-ID: <72B01876-DA02-4306-9994-F7F1F12A7C29@tu-clausthal.de> References: In-Reply-To: Accept-Language: de-DE, en-US Content-Language: de-DE X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [93.231.127.249] Content-Type: text/plain; charset="us-ascii" Content-ID: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Scanned: by Sophos PureMessage 6.4.3.2751440, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2018.5.16.184216, AntiVirus-Engine: 5.49.1, AntiVirus-Data: 2018.5.4.5491002 at tu-clausthal.de X-Spam-Level: (8%, ' HTML_00_01 0.05, HTML_00_10 0.05, BODYTEXTP_SIZE_3000_LESS 0, BODY_SIZE_2000_2999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, IN_REP_TO 0, LEGITIMATE_SIGNS 0, MSG_THREAD 0, NO_URI_HTTPS 0, REFERENCES 0, WEBMAIL_SOURCE 0, WEBMAIL_XOIP 0, WEBMAIL_X_IP_HDR 0, __ANY_URI 0, __BOUNCE_CHALLENGE_SUBJ 0, __BOUNCE_NDR_SUBJ_EXEMPT 0, __CC_NAME 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __DQ_NEG_HEUR 0, __DQ_NEG_IP 0, __FORWARDED_MSG 0, __FRAUD_MONEY_CURRENCY 0, __FRAUD_MONEY_CURRENCY_DOLLAR 0, __HAS_CC_HDR 0, __HAS_FROM 0, __HAS_MSGID 0, __HAS_XOIP 0, __IN_REP_TO 0, __LINES_OF_YELLING 0, __MIME_TEXT_ONLY 0, __MIME_TEXT_P 0, __MIME_TEXT_P1 0, __MIME_VERSION 0, __MSGID_APPLEMAIL 0, __NO_HTML_TAG_RAW 0, __OEM_PRICE 0, __REFERENCES 0, __SANE_MSGID 0, __STOCK_PHRASE_7 0, __SUBJ_ALPHA_END 0, __SUBJ_ALPHA_NEGATE 0, __SUBJ_REPLY 0, __TO_MALFORMED_2 0, __TO_NAME 0, __TO_NAME_DIFF_FROM_ACC 0, __TO_REAL_NAMES 0, __URI_NO_WWW 0, __URI_NS , __ix.dnsbl.manitu.net_ERROR ') List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > Am 16.05.2018 um 20:40 schrieb Adrian Klaver : >=20 > 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 =3D n.idsubstance >> where lower(btrim(n.name)) =3D lower(btrim($1)); >> $BODY$; >> substance and substancetrivialname have got a 1-to-N relationship (for e= ach substance can exist multiple trivial names). >> If I call the function with a non-existing trivial name it returns a sin= gle row with all fields are set to NULL. >=20 > Well I was on the right track for the wrong reason. At any rate SETOF wor= ks: >=20 > select * from cell_per where cell_per =3D 100; > line_id | category | cell_per | ts_insert | ts_update | user_insert | use= r_update | plant_type | season | short_category > ---------+----------+----------+-----------+-----------+-------------+---= ----------+------------+--------+---------------- > (0 rows) >=20 > CREATE OR REPLACE FUNCTION cp(integer) > RETURNS cell_per > LANGUAGE 'sql' > AS $BODY$ > select cell_per.* from cell_per where cell_per =3D $1; > $BODY$; >=20 >=20 > select * from cp(100); > line_id | category | cell_per | ts_insert | ts_update | user_insert | use= r_update | plant_type | season | short_category > ---------+----------+----------+-----------+-----------+-------------+---= ----------+------------+--------+---------------- > NULL | NULL | NULL | NULL | NULL | NULL | NUL= L | NULL | NULL | NULL > (1 row) >=20 >=20 > CREATE OR REPLACE FUNCTION cp(integer) > RETURNS SETOF cell_per > LANGUAGE 'sql' > AS $BODY$ > select cell_per.* from cell_per where cell_per =3D $1; > $BODY$; >=20 >=20 > select * from cp(100); > line_id | category | cell_per | ts_insert | ts_update | user_insert | use= r_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 con= fusing, because imho setof is >=3D 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 =