4

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.

1
  • 2
    return func1(); works for me: sqlfiddle.com/#!15/18442/1 What is the error you get? Commented Sep 6, 2016 at 11:12

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

7 Comments

Using := as the assignment operator is preferred over the simple =
@a_horse_with_no_name why is it prefered?
I never liked the Pascal notation. Just a personal preference :)
Up until 9.3 this was actually the only documented assignment operator: postgresql.org/docs/9.3/static/plpgsql-statements.html a simple equal sign was nevertheless accepted.
@ban - "=" is logical operator, ":=" is assign statement. Some languages that using "=" for assigning use different symbol for equivalence. PLpgSQL has some limits against origin ADA so this difference doesn't play big role, but better to be consistent with original language - the possibility to use "=" for assignment is bug, but it is too late to fix it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.