turn Procedure into Function

Asked

Viewed 44 times

0

I’m trying to turn this procedure in function, to use inside another procedure, but I can’t evolve:

CREATE PROCEDURE bancodados.sp_calculo(param_id_cnpj varchar(14))
BEGIN
    SELECT id_cnpj, 
           AVG(valor)*12 AS fat, 
           regime, 
           aliquota,
           (aliquota*SUM(valor))/100 AS pagamento,
           fun_dia_pagamento(fun_data_pagamento(NOW())) 
    AS data FROM (
      SELECT
        a.valornf,
        a.id_cnpj,              
        b. regime,              
        c. minimo,
        c. maximo,
        c. aliquota             
      FROM  bancodados.tabela_valores a
        LEFT JOIN bancodados.tabela_empresa b
        ON a.id_cnpj = b.id_cnpj
        LEFT JOIN bancodados.tabela_anexos c
        ON b.regime = c.regime AND 
           a.valor >= c.minimo AND 
           a.valor <= c.maximo        
        WHERE (a.id_cnpj = param_id_cnpj AND 
        DATE_FORMAT(data_registro,'%Y-%m') < DATE_FORMAT(fun_mes_anterior(NOW()),'%Y-%m')
        AND 
        DATE_FORMAT(data_registro,'%Y-%m') >= DATE_FORMAT(fun_12meses_antes(NOW()),'%Y-%m')
    ))x;
END $$
  • The current trial is just this select? What you expect to be returned by the new trial?

  • Genos, this project calculates the total of the last 12 months' sales and then returns the payment aliquot. I want to turn it into Function to put inside another Procedure that makes cash flow.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.