When I run my SQL function it returns NULL, even with data entered in the database

Asked

Viewed 59 times

1

I created an SQL function to calculate the user’s daily calories by multiplying their baseline metabolic rate by the level of physical activity. Workbench shows no error when implementing my function, but when I run it returns NULL.

The data entered in the database are:

Table proc_dieta:

met_basal
1872

Table usuario:

sex atv_fisica
Masculine Low

Follows, below, the function created:

CREATE DEFINER=`root`@`localhost` FUNCTION `funcao_jovem`(id INT) 
RETURNS decimal(10,2)
BEGIN

    DECLARE fator_atividade DECIMAL(10,2);
    DECLARE calorias_diarias DECIMAL(10,2);
    DECLARE sexo VARCHAR(15);
    DECLARE atv_fisica VARCHAR(20);
    DECLARE met_basal INT;

    SELECT met_basal, atv_fisica, sexo INTO met_basal, atv_fisica, sexo 
    FROM usuario, proc_dieta 
    WHERE proc_dieta.cod_cad = usuario.cod_cad  
    AND usuario.cod_cad = id;

    IF (sexo = 'Masculino') THEN 
        IF (atv_fisica = 'Baixa') THEN SET fator_atividade = 1.60;
        ELSEIF (atv_fisica = 'Alta') THEN SET fator_atividade = 6.00;
        ELSE SET fator_atividade = 2.5; END IF;
    ELSE 
        IF (atv_fisica = 'Baixa') THEN SET fator_atividade = 1.50;
        ELSEIF (atv_fisica = 'Alta') THEN SET fator_atividade = 6.00;
        ELSE SET fator_atividade = 2.2; END IF;
    END IF;
    
     SET calorias_diarias = met_basal * fator_atividade; 
    
    RETURN calorias_diarias;
END
  • 1

    It seems that the problem is in SELECT.

  • sex INTO met_basal, that’s right?

  • Yes, because the data in INTO follow the same sequence as in SELECT " met_basal, atv_fisica, sex INTO met_basal, atv_fisica, sex "

1 answer

0

I imagine that the query should only return a result, since all the later logic takes into account variables whose value is obtained from the query itself.

The consultation seems to me to have "problems". Maybe something like this works:

CREATE DEFINER='root'@'localhost' FUNCTION 'funcao_jovem'(id INT) 
RETURNS decimal(10,2)
BEGIN

    DECLARE fator_atividade DECIMAL(10,2)
    DECLARE calorias_diarias DECIMAL(10,2)

    SELECT  met_basal
        ,   atv_fisica
        ,   sexo 
    INTO    @met_basal
        ,   @atv_fisica
        ,   @sexo 
    FROM    usuario
        ,   proc_dieta 
    WHERE   proc_dieta.cod_cad  = usuario.cod_cad  
        AND usuario.cod_cad     = id
    LIMIT 1

    IF (sexo = 'Masculino') THEN 
        IF (atv_fisica = 'Baixa') THEN SET fator_atividade = 1.60
        ELSEIF (atv_fisica = 'Alta') THEN SET fator_atividade = 6.00
        ELSE SET fator_atividade = 2.5; END IF
    ELSE 
        IF (atv_fisica = 'Baixa') THEN SET fator_atividade = 1.50
        ELSEIF (atv_fisica = 'Alta') THEN SET fator_atividade = 6.00
        ELSE SET fator_atividade = 2.2; END IF
    END IF
    
    SET calorias_diarias = @met_basal * fator_atividade
    
    RETURN calorias_diarias
END
  • when executing code appears this error " ERROR 1054: Unknown column 'sex' in 'field list' "and when I declare the column said as unknown, it continues to return NULL

Browser other questions tagged

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