Advertisement
LightProgrammer000

Exemplos de Funções [ Declare ]

Nov 22nd, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.58 KB | None | 0 0
  1. /* ------------------------ FUNCTION (DECLARE) ------------------------ */
  2.  
  3. -- Padrao(1)
  4. delimiter $$
  5. create function fn_multiplicacao( i int, j int ) returns int
  6. begin
  7.  
  8.     -- Declaracao de variaveis locais
  9.     declare
  10.    
  11.     /* PROCEDIMENTO */
  12.    
  13.     return (/* Valor */);
  14.  
  15. end $$
  16. delimiter ;
  17.  
  18. /* ------------------------ Function(1) ------------------------ */
  19. delimiter $$
  20. create function fn1_desconto_percentual( cod int, taxa decimal(10,2) ) returns decimal(10,2)
  21. begin
  22.  
  23.     -- Declaracao de variaveis
  24.     declare preco decimal(10,2);
  25.  
  26.     -- Selecionando e atribuindo na variavel 'preco'
  27.     select
  28.         tl.preco_livro
  29.     from
  30.         tbl_livro tl
  31.     where
  32.         tl.id_livro = cod
  33.     into
  34.         preco;
  35.  
  36.     -- Retornado o valor descontado
  37.     return ( preco * ( 1 - taxa / 100 ) );
  38.  
  39. end $$
  40. delimiter ;
  41.  
  42. -- Exibicao de valor:
  43. select tl.preco_livro from tbl_livro tl where tl.id_livro = 1;
  44.  
  45. -- Chamada de Funcao:
  46. select fn1_desconto_percentual(1,10);
  47.  
  48. /* ------------------------ Function(2) ------------------------ */
  49. delimiter $$
  50. create function fn2_aumento_percentual( cod int, taxa decimal(10,2) ) returns decimal(10,2)
  51. begin
  52.    
  53.     -- Declaracao de variaveis
  54.     declare preco decimal(10,2);
  55.    
  56.     -- Selecionando e atribuindo na variavel 'preco'
  57.     select
  58.         tl.preco_livro
  59.     from
  60.         tbl_livro tl
  61.     where
  62.         tl.id_livro = cod
  63.     into
  64.         preco;
  65.    
  66.     -- Retornado o valor descontado
  67.     return( preco * ( 1 + taxa / 100 ) );
  68.  
  69. end $$
  70. delimiter ;
  71.  
  72. -- Exibicao de valor:
  73. select tl.preco_livro from tbl_livro tl where tl.id_livro = 1;
  74.  
  75. -- Chamada de Funcao:
  76. select fn2_aumento_percentual(1,10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement