PostgreSQL/Postgre SQL/Declare Variable
Using variable declaration options
postgres=# -- Using variable declaration options
postgres=#
postgres=# CREATE FUNCTION example_function () RETURNS text AS "
postgres"# DECLARE
postgres"#
postgres"# -- Declare a constant integer with a default value of 5.
postgres"# five CONSTANT INTEGER := 5;
postgres"#
postgres"# -- Declare an integer with a default value of 100 that cannot be NULL.
postgres"# ten INTEGER NOT NULL := 10;
postgres"#
postgres"# -- Declare a character with a default value of "a".
postgres"# letter CHAR DEFAULT ""a"";
postgres"#
postgres"# BEGIN
postgres"# return letter;
postgres"# END;
postgres"# " LANGUAGE "plpgsql";
CREATE FUNCTION
postgres=#
postgres=# select example_function ();
example_function
------------------
a
(1 row)
postgres=#
postgres=# drop function example_function ();
DROP FUNCTION
postgres=#
postgres=#
Variable Declarations
postgres=# -- Variable Declarations
postgres=#
postgres=# CREATE FUNCTION identifier () RETURNS int4 AS "
postgres"# DECLARE
postgres"#
postgres"# -- Declare an integer.
postgres"# subject_id INTEGER;
postgres"#
postgres"# -- Declare a variable length character.
postgres"# book_title VARCHAR(10);
postgres"#
postgres"# -- Declare a floating point number.
postgres"# book_price FLOAT;
postgres"#
postgres"# BEGIN
postgres"# return 10;
postgres"# END;
postgres"# " LANGUAGE "plpgsql";
ERROR: function "identifier" already exists with same argument types
postgres=#
postgres=# select identifier();
identifier
------------
10
(1 row)
postgres=#
postgres=# drop function identifier();
DROP FUNCTION
postgres=#
postgres=#