ban
ban

Reputation: 197

plpgsql function return another function value

I have a function func1() which returns integer.

I want to write another function func2(mode integer) which can return func1() reults or do some more stuff. The return value of func1() is of type INTEGER.

something like this:

CREATE OR REPLACE FUNCTION func2(mode integer)
  RETURNS integer AS
$$
begin
     if mode=1 then
       return func1();  -- NOT plpgsql syntax
     end if;

     more stuff .....

     return 2;
end
$$
LANGUAGE plpgsql VOLATILE

my question is how to do return func1(); ?

I know I can do :

select func1() into temp;
return temp;

but I was wondring if there is a more elegent way to do that.

Upvotes: 4

Views: 624

Answers (1)

Boris Schegolev
Boris Schegolev

Reputation: 3701

All of these work:

Option 1:

CREATE OR REPLACE FUNCTION func2(mode integer)
    RETURNS integer AS
$BODY$
DECLARE 
    _result integer;
BEGIN
    _result = 2;
     IF mode=1 THEN
       _result = func1();
     END IF;

     --more stuff .....

     RETURN _result;
END
$BODY$
    LANGUAGE plpgsql VOLATILE COST 100;

Option 2:

CREATE OR REPLACE FUNCTION func2(mode integer)
    RETURNS integer AS
$BODY$
BEGIN
     IF mode=1 THEN
       RETURN func1();
     END IF;

     --more stuff .....

     RETURN 2;
END
$BODY$
    LANGUAGE plpgsql VOLATILE COST 100;

Upvotes: 2

Related Questions