0

I am extremely new to postgresql and do not understand how to set the value of a variable in one function with the value from another function. This is as far as I have got:

Function A

CREATE FUNCTION functiona(_postid integer) RETURNS integer
        LANGUAGE SQL
    AS
$$
BEGIN
    SELECT posttypeid
    FROM post
    WHERE postid = _postid;
END;
$$;

Function B (which calls Function A to get value)

CREATE OR REPLACE FUNCTION functionb(_postid integer DEFAULT NULL) RETURNS text
    LANGUAGE plpgsql
AS
$$
DECLARE _posttypeid int;
BEGIN
    IF _postid IS NOT NULL
    THEN
           _posttypeid := select functiona(_postid); -- want to set value of _posttypeid to the value that comes from functiona
    END IF;
END;
$$;

In Function B, how do I set the value of _posttypeid to be the value that gets returned from Function A?

volume one
  • 6,800
  • 13
  • 67
  • 146
  • First why not have just one function? Two the first will not work as it is `LANGUAGE SQL` and that does not support `BEGIN/END`(that is for `LANGUAGE plpgsql`). – Adrian Klaver Jan 09 '21 at 19:41
  • 1
    `_posttypeid := functiona(_postid);` –  Jan 09 '21 at 20:05

0 Answers0