Re: plpgsql function not working - Mailing list pgsql-general

From Michael Fuhr
Subject Re: plpgsql function not working
Date
Msg-id 20050425200856.GA3170@winnie.fuhr.org
Whole thread Raw
In response to plpgsql function not working  ("Ruff, Jeffry C. SR." <jeffry.ruff@tycoelectronics.com>)
List pgsql-general
On Mon, Apr 25, 2005 at 02:40:29PM -0500, Ruff, Jeffry C. SR. wrote:
>
> CREATE FUNCTION userinfo.group_list(text) RETURNS text AS'
>    DECLARE
>     rec RECORD;
>     string text := NULL;
>    BEGIN
>     FOR rec IN SELECT * FROM userinfo.userdb_groups WHERE username = $1 LOOP
>            string := string || rec.groupname || \',\';

The result of "NULL || anything" is NULL.  Try initializing "string"
to an empty string instead of NULL.

In 7.4 and later you can use an array constructor and array_to_string():

SELECT array_to_string(array(
  SELECT groupname FROM userinfo.userdb_groups WHERE username = 'jruff'
), ',');

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-general by date:

Previous
From: John DeSoi
Date:
Subject: Re: plpgsql function not working
Next
From: John DeSoi
Date:
Subject: Re: plpgsql function not working