Re: Return Record

From: Joe Conway <mail(at)joeconway(dot)com>
To: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Return Record
Date: 2003-06-07 16:05:37
Message-ID: 3EE20D51.9060804@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice

Rory Campbell-Lange wrote:
> I'm not clear on how to handle returning a record from a function.
> I have planned a function that is handed two strings and returns two
> integers. I need to return errors that satisfy the return type. At the
> moment my "RETURN 0;" lines result in "return type mismatch..." errors.
>

You can't return type "record" in the ways you were trying (RETURN (0,
0); and RETURN 0;). Also, since you were raising EXCEPTION instead of
NOTICE, the function would never return anyway. See below -- I think it
does what you want:

CREATE OR REPLACE FUNCTION fn_b1_login2(varchar, varchar)
RETURNS record AS'
DECLARE
email ALIAS for $1;
pass ALIAS for $2;
recone RECORD;
BEGIN
-- more extensive checking to be done in client program
IF email IS NULL THEN
RAISE NOTICE ''no email found at fn_e3_person_register'';
SELECT into recone 0,0;
RETURN recone;
END IF;

IF pass IS NULL THEN
RAISE NOTICE ''no pass found at fn_e3_person_register'';
SELECT into recone 0,0;
RETURN recone;
END IF;

--
SELECT INTO recone 1,2;
-- p.n_id as nid, b.n_id as bid
-- FROM
-- people p, boards b
-- WHERE
-- p.t_email = email
-- AND
-- p.t_password = pass
-- AND
-- p.n_id = b.n_creator
-- AND
-- b.n_type = 0;

IF NOT FOUND THEN
RAISE NOTICE ''no person board combination found at
fn_e3_person_register'';
SELECT into recone 0,0;
RETURN recone;
END IF;

RETURN recone;
END;
' LANGUAGE 'plpgsql';

regression=# select * from fn_b1_login2('a', null) as (pid int, bid int);
NOTICE: no pass found at fn_e3_person_register
pid | bid
-----+-----
0 | 0
(1 row)

regression=# select * from fn_b1_login2(null, 'b') as (pid int, bid int);
NOTICE: no email found at fn_e3_person_register
pid | bid
-----+-----
0 | 0
(1 row)

regression=# select * from fn_b1_login2('a', 'b') as (pid int, bid int);
pid | bid
-----+-----
1 | 2
(1 row)

HTH,
Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ian Barwick 2003-06-07 17:34:28 Re: update phenomenom
Previous Message Peter Eisentraut 2003-06-07 16:01:42 Re: Backups and restores.

Browse pgsql-novice by date

  From Date Subject
Next Message Jean-Christian Imbeault 2003-06-08 06:59:35 Re: Question regarding keyword checkboxes in database design
Previous Message Richard Huxton 2003-06-07 08:46:05 Re: Return Record