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 1fJ1LT-00049P-G4 for pgsql-general@arkaria.postgresql.org; Wed, 16 May 2018 18:40:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fJ1LS-0002ha-BX for pgsql-general@arkaria.postgresql.org; Wed, 16 May 2018 18:40:14 +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 1fJ1LS-0002hM-2p for pgsql-general@lists.postgresql.org; Wed, 16 May 2018 18:40:14 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJ1LN-0001gr-29 for pgsql-general@postgresql.org; Wed, 16 May 2018 18:40:13 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 94E9722124; Wed, 16 May 2018 14:40:06 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 16 May 2018 14:40:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=yngwGsJxchsAa+wV6tFWPQUvhLWrC 4VFmUJ0zb4oYZo=; b=Fci7a8qmbOS9H66hbsTIhWmCumCr2cz0TOyQYnVPbsynt ZDbrI7ZoVypkdbzDoKcr8E9Hcv/v2NL4TR/rTGWm3HloG0gMJjiXwCIr4KuXQHl+ zBr3zmk2lDvyWQ1IkdpMJ75BzI5v57mTOaOBxANO2XxuNMUq2vRzKfkwfzihCUQ+ 6WZeGqN8d7CeOxVD63ycXd2aw53Yhv2WIz4zA8MDMkUAS7dYJRAF4mOngpkuVd8z k6CVdLBGYe6Vp4CmUxl7g7CQar3DKk4315eY+AN5upwIb2AQAfszOsnmyntF5vJJ 9EatouJE/YrGOKgrXgctoWgKm7EVHRNc+70LwaYOw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=yngwGs JxchsAa+wV6tFWPQUvhLWrC4VFmUJ0zb4oYZo=; b=e4od/xu8FKLfAk9f5HQGEv YtGch/3zlz9TvIW6ohOyE7t96DpFnz4lreNhK5Nov41CNNePU+WRxDf5A/NwniIj aMcJy7OD/LSMm2M0FWigWPZq6rN6vY0xL9BcWBEnYXzXOgWB4UR+vfA5XDCMHKv9 k4H/2h0sB/FF9zFr+bbsmJ4vSW+YFM70Rl0imo/C2rvP3oZgJqYd5j4OT4wpjEXu MwQdeSvDwKmE0sGUGgl9dzm1rBInNBTAj4Ps4q4O8p3w4nB23+cW2/fi3mYah8Xs nhvJuSTdJpChrywMRS0jcZcGjp27ufFBXbURj1sRt1WOiIvcLmApdRc0lpRlxRJg == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id F0B8B10263; Wed, 16 May 2018 14:40:05 -0400 (EDT) Subject: Re: sql function with empty row To: Philipp Kraus , "pgsql-general@postgresql.org" References: From: Adrian Klaver Message-ID: Date: Wed, 16 May 2018 11:40:05 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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) > 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