Declaring Local Variables In PL/pgSQL Functions And Procedures
Learn how to declare local variables in PL/pgSQL functions with :=, = and DEFAULT. Understand type-only parameters, aliases and table column types.
Buy Me a Coffee☕
You can declare local variables with :=, = in DECLARE clause as shown below:
CREATE FUNCTION my_func()
RETURNS INT
AS $$
DECLARE
value1 INT := 1; -- Here
value2 INT = 2; -- Here
value3 INT DEFAULT 3; -- Here
value4 CONSTANT INT := 4; -- Here
value5 INT; -- Here
BEGIN
RETURN value1 + value2 + value3;
END;
$$ LANGUAGE plpgsql;
*Memos:
:=, = and DEFAULT are the same.
Trying to change the constant local variable value4 gets error.
The uninitialized local variable value5 is NULL.
You can declare local variables with DECLARE clause in a PL/...