Advertisement
fahadkalil

plpgsql_funcao_desconto

Mar 25th, 2021
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. CREATE OR REPLACE FUNCTION aula03.f_desconto(in_valor NUMERIC, in_desconto NUMERIC)
  3. RETURNS NUMERIC AS $$
  4. DECLARE
  5.     -- Declaracao de variavel
  6.     v_resultado NUMERIC := 0.0;
  7. BEGIN
  8.     IF in_desconto <= 100 AND in_desconto >= 0 THEN
  9.         v_resultado := in_valor * (1.0 - (in_desconto / 100));
  10.     ELSE
  11.         RAISE EXCEPTION 'Desconto inválido'
  12.         USING HINT = 'Valores aceitos: entre 0.0 e 100.0';
  13.     END IF;
  14.  
  15.     RETURN v_resultado;
  16. END;
  17. $$ LANGUAGE plpgsql;
  18.  
  19. /* Chamando a function */
  20. SELECT aula03.f_desconto(100, 10);
  21. --SELECT aula03.f_desconto(150, 101);
  22. --SELECT TRUNC(aula03.f_desconto(100, 10), 2);
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement