Re: plpgsql function not working

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "Ruff, Jeffry C(dot) SR(dot)" <jeffry(dot)ruff(at)tycoelectronics(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql function not working
Date: 2005-04-25 20:08:56
Message-ID: 20050425200856.GA3170@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Wolff III 2005-04-25 20:10:46 Re: Calculated bigserial column in a view
Previous Message Typing80wpm 2005-04-25 20:01:37 Toad Adm. Datasource Location