pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedUnexpected Return from Function
3+ messages / 2 participants
[nested] [flat]
* Unexpected Return from Function
@ 2001-12-02 02:00 Anthony Bouvier <anthony@developware.com>
2001-12-02 02:23 ` Re: Unexpected Return from Function Stephan Szabo <sszabo@megazone23.bigpanda.com>
0 siblings, 1 reply; 3+ messages in thread
From: Anthony Bouvier @ 2001-12-02 02:00 UTC (permalink / raw)
To: pgsql-sql
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
---------------
---------------
If I get rid of the concatenation, like so:
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 := my_record.login;
END LOOP;
RETURN responsible_list;
END;
' LANGUAGE 'plpgsql';
I receive last result (for id = 2), like so:
get_responsible
---------------
mary
---------------
The SELECT statement itself runs fine, so I know it is returning two
records.
Also, the reason I am passing a 'text' datatype to the function, is
because I'd ultimately like to have the "WHERE id IN" statement to be
dynamic, like so:
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) LOOP
responsible_list := my_record.login;
END LOOP;
RETURN responsible_list;
END;
' LANGUAGE 'plpgsql';
If anyone can help me with this, I'd be much appreciative. I've been
trying combination after combination of things to try and resolve this
for the past 6 and a half hours.
Thanks,
Anthony "pulling his hair out" Bouvier
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Unexpected Return from Function
2001-12-02 02:00 Unexpected Return from Function Anthony Bouvier <anthony@developware.com>
@ 2001-12-02 02:23 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
2001-12-02 20:43 ` Re: Unexpected Return from Function Anthony Bouvier <anthony@developware.com>
0 siblings, 1 reply; 3+ messages in thread
From: Stephan Szabo @ 2001-12-02 02:23 UTC (permalink / raw)
To: Anthony Bouvier <anthony@developware.com>; +Cc: pgsql-sql
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.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Unexpected Return from Function
2001-12-02 02:00 Unexpected Return from Function Anthony Bouvier <anthony@developware.com>
2001-12-02 02:23 ` Re: Unexpected Return from Function Stephan Szabo <sszabo@megazone23.bigpanda.com>
@ 2001-12-02 20:43 ` Anthony Bouvier <anthony@developware.com>
0 siblings, 0 replies; 3+ messages in thread
From: Anthony Bouvier @ 2001-12-02 20:43 UTC (permalink / raw)
To: Stephan Szabo <sszabo@megazone23.bigpanda.com>; +Cc: 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@postgresql.org
[mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Stephan Szabo
Sent: Saturday, December 01, 2001 9:23 PM
To: Anthony Bouvier
Cc: pgsql-sql@postgresql.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@postgresql.org so that your
message can get through to the mailing list cleanly
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-12-02 20:43 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-12-02 02:00 Unexpected Return from Function Anthony Bouvier <anthony@developware.com>
2001-12-02 02:23 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
2001-12-02 20:43 ` Anthony Bouvier <anthony@developware.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox