Exemplo de função SQL escalar
O seguinte exemplo cria uma função que compara dois números e retorna o maior valor. Para ter mais informações, consulte CREATE FUNCTION.
create function f_sql_greater (float, float)
returns float
stable
as $$
select case when $1 > $2 then $1
else $2
end
$$ language sql;
A consulta a seguir chama a nova função f_sql_greater para consultar a tabela SALES e retornar COMMISSION ou 20% de PRICEPAID, o que for maior.
select f_sql_greater(commission, pricepaid*0.20) from sales;