Something like this:
CREATE OR REPLACE FUNCTION get(param_id integer)
RETURNS integer AS
$BODY$
BEGIN
SELECT col1 FROM TABLE WHERE id = param_id;
END;
$BODY$
LANGUAGE plpgsql;
I would like to avoid a DECLARE just for this.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes you can. There is a number of ways.
1)
RETURN (SELECT ...)2) Use an
OUTorINOUTparameterMore in the manual here.
3) (Ab)use
INparameterSince Postgres 9.0 you can also use input parameters as variables. The release notes for 9.0:
Variants 2) and 3) do use a variable implicitly, but you don’t have to
DECLAREone explicitly (as requested).4) Use a
DEFAULTvalue with anINOUTparameterThis is a bit of a special case. The function body can be empty.
INOUT _col1 integer = 123is short notation forINOUT _col1 integer DEFAULT 123. See:5) Use a plain SQL function instead
Or use use param reference
$1instead of param name.Variant 5) one uses plain single quotes for the function body. All the same. See:
fiddle – demonstrating all (incl. call)