Writing a Postgresql Stored Procedure Returning a Single Value

Say we have a table called person with the following structure

---------------------------
column| datatype
---------------------------
 _id  | autonumber
 name | text
 age  | integer
---------------------------

We want to write a stored procedure to return the highest age that we have in the table. We will use OUT param here.

CREATE OR REPLACE FUNCTION get_max_age(OUT max_age int)
    RETURNS int
    LANGUAGE 'plpgsql'
AS $BODY$

BEGIN
	SELECT MAX(age) INTO max_age FROM person;
END

$BODY$;

That’s it.


Other postgresql articles