Procedures MYSQL - Pass parameters or not

Asked

Viewed 84 times

0

I created a procedure to make the following process: If you do not enter the required parameter, you will bring the complete list of what I want (in this case a list of students etc). If you type, it will bring the specific student. But instead, she only brings it back if I type the id of the student, ignoring the condition IF P_idAluno IS NULL THEN.

 BEGIN IF (P_idAluno IS NULL)
  THEN SELECT A.idAluno AS CodigoAluno,
  A.NomeAluno AS Nome, A.SexoAluno AS Sexo, A.DataNascimento, T.Turma,
  S.Serie, T.Turno 
  FROM tbAluno A 
  INNER JOIN tbTurma T ON T.idTurma = A.id_Turma 
  INNER JOIN tbSerie S ON S.idSerie = T.id_Serie 
  ORDER BY A.NomeAluno; 
 ELSE 
  SELECT A.idAluno AS CodigoAluno, A.NomeAluno AS
  Nome, A.SexoAluno AS Sexo, A.DataNascimento, T.Turma, S.Serie, T.Turno
  FROM tbAluno A 
  INNER JOIN tbTurma T ON T.idTurma = A.id_Turma
  INNER JOIN tbSerie S ON S.idSerie = T.id_Serie WHERE idAluno = P_idAluno;
 END IF;
END
  • 1

    try something like ... idAluno = COALESCE(P_idaluno , idAluno ) in Where , passed the parameter looks for a student , null past behind all

  • How are you calling Procedure? P_idaluno is being passed as either null or 0?

  • I’m calling the precedent: spAlunoTurma. CREATE PROCEDURE'spAlunoTurma'(P_idaluno INT)[...]

1 answer

0

Easiest way to do it is like this:

    BEGIN 

      SELECT A.idAluno AS CodigoAluno, A.NomeAluno AS
      Nome, A.SexoAluno AS Sexo, A.DataNascimento, T.Turma, S.Serie, T.Turno
      FROM tbAluno A 
      INNER JOIN tbTurma T ON T.idTurma = A.id_Turma
      INNER JOIN tbSerie S ON S.idSerie = T.id_Serie WHERE idAluno = COALESCE(P_idAluno,A.idAluno);

    END;
  • Thanks for the help friend! But it gave a similar problem.

  • what would be the problem?

  • He doesn’t return anything when I don’t put on some parameter, you know? When I type '1' for example, it returns idAluno 1 with all fields of SELECT. But when I don’t put it doesn’t bring me anything. That’s why I put the IF P_idaluno IS NULL then

Browser other questions tagged

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