Assigning default value for type

2019-08-13 12:31发布

问题:

How to assign default value to following type of Oracle statement into PostgreSQL 9.3?

CREATE OR REPLACE FUNCTION(....
...
DECLARE
 v_var Table01.column01%TYPE := 'SLOW';
BEGIN
...
...
END;

回答1:

Postgres allows to provide parameter defaults, which kick in for missing parameters in the function call. Only allowed for parameters at the end of the list.
Example:

CREATE OR REPLACE FUNCTION foo (
                  param1 int
                , v_char tbl01.col01%TYPE DEFAULT 'foo')
...
-- no need to DECLARE anything else.
BEGIN
...

Syntactical shortcut would be v_char tbl01.col01%TYPE = 'foo'.

Call:

SELECT * FROM foo(1, 'bar');
SELECT * FROM foo(1);        -- Param default kicks in

Details in the manual.