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

Reply via email to