Advertisement
LightProgrammer000

Funcao [ Case_When ]

Nov 21st, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.06 KB | None | 0 0
  1. /* ------------------------ FUNCTION (DECLARE) ------------------------ */
  2.  
  3. -- Padrao(1)
  4. delimiter $$
  5. create function
  6. begin
  7.  
  8.     -- Declaracao de variaveis locais
  9.     declare
  10.  
  11.     /* PROCEDIMENTO */
  12.    
  13.     -- Retorno de valor:
  14.     return(/* Valor */)
  15.    
  16. end $$
  17. delimiter ;
  18.  
  19. /* ------------------------ Function(1) ------------------------ */
  20. delimiter $$
  21. create function fn2_calcula_imposto( salario decimal(8,2) ) returns decimal(8,2)
  22. begin
  23.  
  24.     -- Variaveis locais
  25.     declare imposto decimal(8,2);
  26.    
  27.     -- Estrutura de decisao: Case
  28.     case
  29.         when( salario < 1000.00 )
  30.         then
  31.             set imposto = salario * 0;
  32.  
  33.         when( salario < 2000.00 )
  34.         then
  35.             set imposto = salario * 0.15;
  36.  
  37.         when( salario < 3000.00 )
  38.         then
  39.             set imposto = salario * 0.22;
  40.  
  41.         else
  42.             set imposto = salario * 0.27;
  43.  
  44.     end case;
  45.        
  46.     -- Retorno
  47.     return( imposto );
  48.  
  49. end $$
  50. delimiter ;
  51.  
  52. -- Chamada de funcao:
  53. select fn2_calcula_imposto(10.00);
  54. select fn2_calcula_imposto(1000.00);
  55. select fn2_calcula_imposto(2000.00);
  56. select fn2_calcula_imposto(3000.00);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement