Mysql Function returning NULL

Asked

Viewed 193 times

1

My faith works for nothing in this world

The SQL is like this:

-- Function used to return the remaining space of a chosen folder. --

DELIMITER $$
CREATE FUNCTION tamanhoRestante (_idPasta INT)
RETURNS INT
BEGIN

    DECLARE lpasTamanho INT;      
    DECLARE lpasUsado INT;
    -- Aqui é verificado o tamanho TOTAL da pasta escolhida. --
    SELECT tamanho FROM pasta WHERE idPasta = _idPasta INTO lpasTamanho;
    -- E agora a verificação de todos os arquivos alocados nesta pasta. --
    IF(SELECT COUNT(*) FROM usuario JOIN usuario_has_pasta WHERE idUsuario = usuario_has_pasta.Usuario_idUsuario > 0) THEN
    SELECT SUM(arquivo.tamanho) FROM arquivo JOIN Pasta_has_Arquivo JOIN Pasta WHERE idArquivo = Arquivo_idArquivo AND Pasta_idPasta = _idPasta INTO lpasUsado;
    ELSE SET lpasUsado = 0;
    END IF;
    -- Agora faz-se o retorno do tamanho total da pasta subtraindo o total utilizado --
RETURN lpasTamanho - lpasUsado;

END
$$
DELIMITER ;

In this test, the lpasTotal = Tamanho total of the selected folder, and lpasUsado is the sum of the size of all files linked to this folder (via a ternary Pasta_has_Arquivo).

The lpasTamanho in the test has size 100, and the lpasTamanho I did the test with 0 and 50, but in both returns Null :/

Can anyone help me with that? I can’t find the mistake.

  • Add in question the result of the select of the tables by doing the favor

  • The select is quite confused. But to feed the variable from select do something like this: SELECT size INTO lpasSize FROM folder WHERE idPasta = _idPasta;

  • I believe this one link solves your problem, see how INTO is done.

  • what you can also do is use the SET in the INTO login.

  • Thanks for the personal answers! The problem is not the select, I took his test and it is ok, the problem is in the second part, the SUM.. :/

1 answer

1


Your select has some problems:

  • The instruction INTO must be shortly after the declaration of the select;
  • The field idPasta is with _ in Join;
  • Prefer to use inner join;

It would look more or less like this after some corrections:

-- Function utilizada para retornar o espaço restante de uma pasta escolhida. --
DELIMITER $$
CREATE FUNCTION tamanhoRestante (_idPasta INT)
RETURNS INT
BEGIN
  DECLARE lpasTamanho INT;
  DECLARE lpasUsado INT;

  -- Aqui é verificado o tamanho TOTAL da pasta escolhida. --
  SELECT tamanho
    INTO lpasTamanho
    FROM pasta
   WHERE idPasta = _idPasta;

  SET lpasUsado = 0;

  -- E agora a verificação de todos os arquivos alocados nesta pasta. --
  IF (EXISTS(SELECT 1 = 1
               FROM usuario u
               INNER JOIN usuario_has_pasta uhp ON u.idUsuario = uhp.Usuario_idUsuario)) THEN
  BEGIN
    SELECT SUM(COALESCE(arquivo.tamanho, 0))
      INTO lpasUsado
      FROM arquivo a
      LEFT JOIN Pasta_has_Arquivo pha ON a.idArquivo = pha.Arquivo_idArquivo
      LEFT JOIN Pasta p ON pha.Pasta_idPasta = p.idPasta;
  END;
  END IF;

  -- Agora faz-se o retorno do tamanho total da pasta subtraindo o total utilizado --
  RETURN lpasTamanho - lpasUsado;
END
$$
DELIMITER ;

The instruction EXISTS checks if the reference returns true, so it is necessary to put 1=1, which will be executed only if the where be satisfied.

  • Thank you very much @Sorack! I noticed some mistakes I had made with your comment, thank you! But it still returns NULL if there is nothing in the Past_has_file, if I add a file there and call this function, it returns the correct size (It was supposed to show 100 in case it was not in an allocated file, but if I allocate a file with size 5, it shows the exact 95).

  • And I’m sorry if I’m being "lazy", but could you kindly explain to me what this would be (SELECT 1 = 1 FROM usuario) ? Thanks again friend!

  • I will change here to be more usable and you test again please. I will also add the explanation of exists. If it works please approve the answer so that someone who has a similar question can benefit

  • Changed and added exists explanation. Now should not return null anymore

  • Thanks again for your time Sorack! Unfortunately it keeps returning NULL.. If I give a SELECT * FROM folder it returns the values id - size 1 - 100 2 - 500 3 - 500 But if I give a select size(1) it returns NULL (just like using ids 2 and 3), but if I add any file in the folder, it returns the right value, example: addArchive(pasta1, file1) (assuming this file is 20 in size) remaining size(pasta1) returns 80..

  • Tries to replace "SELECT SUM(file.size)" with "SELECT SUM(COALESCE(file.size, 0))"

  • I also tested this COALESCE and returns NULL.. I think for now I will give it up like this.. I am 2 hours trying to fix this, I have already made changes in the entire database, in all triggers, I have tested with nothing in the database only this Function, and always the same result, if you have nothing allocated the folder in the folder has_file, it returns null, if you have more than one file, it sums and returns the value exactly as it should, the only problem is it is returning null when there is nothing in the has_file folder with the folder id.. : / Thank you very much Sorack, and sorry to bother you..

  • Oh yes, I already know what the Kauan problem is, in the second select change the INNER to LEFT and keep the COALESCE

Show 3 more comments

Browser other questions tagged

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