PL/pgSQL question
PL/pgSQL question
От:
"Sebastian Ritter" <ritter.sebastian@gmail.com>
Дата:
Hi all,
I have a question regarding functions. How can I return zero rows from a function whose return type is a table row? I did the following test and it did not work as expected:
CREATE OR REPLACE FUNCTION
fn_get_user (integer) RETURNS usertable AS '
DECLARE
in_userid ALIAS for $1;
resulter usertable%ROWTYPE;
BEGIN
IF in_userid IS NULL THEN
RAISE EXCEPTION ''No user provided'';
RETURN null;
END IF;
SELECT INTO resulter
usertable.*
FROM
usertable
WHERE
id = in_userid;
IF FOUND THEN
RETURN resulter;
ELSE
RETURN null;
END IF;
END;'LANGUAGE plpgsql;
>select * from fn_get_user(-1);
id | col1 | col2 | col3| name | email
----+------------+-------------+------------+--------
| | | | | |
(1 row)
This returns a null row. I am trying to make it behave such that it returns zero rows like a straight select.
>select * from usertable where id =-1;
id | col1 | col2 | col3| name | email
----+------------+-------------+------------+--------
(0 rows)
Is this possible in anyway?
Regards,
Sebastian
Re: PL/pgSQL question
От:
"Milen A. Radev" <milen@radev.net>
Дата:
Sebastian Ritter написа:
> Hi all,
>
> I have a question regarding functions. How can I return zero rows from a
> function whose return type is a table row? I did the following test and it
> did not work as expected:
[...]
CREATE OR REPLACE FUNCTION foobar(boolean) RETURNS SETOF x AS $_$
DECLARE res x%ROWTYPE;
BEGIN
if $1 THEN RETURN NEXT res; ELSE RETURN; END IF;
END;$_$
LANGUAGE plpgsql;
--
Milen A. Radev