Les fonctions: les conditions
À l’intérieur d’une fonction PL/PgSQL, il est possible de faire appel à des conditions (IF, ELSEIF, ELSE et END IF)
tutorial=> create or replace function tutorial(text)
returns int
as $$
DECLARE
a_value alias for $1;
v_return INT;
BEGIN
IF a_value = '' THEN
v_return = 0;
ELSEIF a_value = 'un' THEN
v_return = 1;
ELSE
v_return = -1;
END IF;
return v_return;
END$$ language plpgsql;
CREATE FUNCTION
tutorial=> select tutorial('');
tutorial
----------
0
(1 row)
tutorial=> select tutorial('un');
tutorial
----------
1
(1 row)
tutorial=> select tutorial('deux');
tutorial
----------
-1
(1 row)
tutorial=>