M
M
muhasa2017-12-21 11:04:25
MySQL
muhasa, 2017-12-21 11:04:25

Why doesn't mysql allow a local variable to be used in a stored procedure?

Hello.
I'm trying to figure out stored procedures and variables in mysql.
It seems that the difference between a local and a system variable is only in the scope, but specifically in this stored procedure, I can’t replace the system variable with a local one in any way!
How is this possible to change? And can anyone change this vulnerable procedure so that a local variable is used instead of a system variable?

BEGIN
DECLARE ap1 TEXT;
DECLARE q1 VARCHAR(255);
SET @q1:= "SELECT t2.id visit_id, DATE_FORMAT(t2.date, '%d.%m.%Y %H:%i') visitdate,CONCAT(t1.name, ' ', t1.surname) doctor_name, t4.name disease_name, CONCAT(t3.name, ' ', t3.surname) pacient_name, t2.complaint as complaint, t2.treatment as treatment, t2.number_room as number_room, t2.visited as visited
      FROM hospital.doctor t1 
      INNER JOIN registration.visit t2 
      ON t1.id = t2.id_doctor
      INNER JOIN registration.pacient t3
      ON t2.id_pacient = t3.id
      LEFT JOIN registration.disease t4
      ON t2.id_disease = t4.id";
IF doctor_id != '' THEN SET @q1:=CONCAT(@q1," WHERE t2.id_doctor = ", doctor_id); END IF;            
PREPARE qr FROM @q1;
EXECUTE qr;
END

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question