Re: Unexpected Return from Function

From: "Anthony Bouvier" <anthony(at)developware(dot)com>
To: "Stephan Szabo" <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Unexpected Return from Function
Date: 2001-12-02 20:43:36
Message-ID: CBEBKIKGCCOCPDEMKBBJMEPKCAAA.anthony@developware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

That was the exact problem. Your help is much appreciated.

Of course, after getting this solution working in respect to the
concatenation, I realise I was going about this whole thing all wrong.
I spent 6 hours trying to do the original with no luck, then came in
today and created an AGGREGATE that does -exactly- what I wanted to do
in the first place, and in a much simpler fashion.

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of Stephan Szabo
Sent: Saturday, December 01, 2001 9:23 PM
To: Anthony Bouvier
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Unexpected Return from Function

On Sat, 1 Dec 2001, Anthony Bouvier wrote:

> I have a FUNCTION:
>
> CREATE FUNCTION get_responsible(text)
> RETURNS TEXT AS '
> DECLARE
> responsible_list text;
> my_record RECORD;
> BEGIN
> FOR my_record IN SELECT login FROM employee WHERE id IN (1,2) LOOP
> responsible_list := responsible_list || '', '' my_record.login;
> END LOOP;
> RETURN responsible_list;
> END;
> ' LANGUAGE 'plpgsql';
>
> The employee table is such:
>
> id | login
> -------------
> 1 | anthony
> 2 | mary
> -------------
>
> I expect the SQL statement "SELECT get_responsible('1,2')" to return
> something like so:
>
> get_responsible
> ---------------
> anthony, mary
> ---------------
>
> But instead I receive:
>
> get_responsible
> ---------------
>
> ---------------

You probably need to initialize responsible_list to an empty string.
My guess is that it starts NULL and NULL concatenated with anything
is still NULL.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
message can get through to the mailing list cleanly

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message postgresql 2001-12-02 21:48:11 Re: problems with single quotes..
Previous Message Tom Lane 2001-12-02 15:00:22 Re: problems with single quotes..