Store date and time in variable in a MySQL stored procedure


I have a problem, I am trying to store the current date and time of the server in a variable, within a procedure stored in MySQL, only I can not do it, the previous thing I do, since in multiple rows I will execute a relative condition to the current date and time and it would be an over load to make a select now () in each row. I am trying it in the following way but it is not stored, it is only shown:

CREATE DEFINER='Cliente'@'%' PROCEDURE 'TblEmpleados'()
select @mivariable := now();
asked by Carlos Daniel Zárate Ramírez 07.09.2018 в 22:40

2 answers



DECLARE mi_variable;
SET mi_variable = SELECT NOW();

since according to the official documentation you are not making the declaration of the variable correctly, nor the assignment.

Official Documentation

answered by 07.09.2018 в 22:44

I already did it, first you have to declare the variable and then through a query and the command assign the data to the variable:

CREATE DEFINER='Cliente'@'%' PROCEDURE 'new_procedure'()
DECLARE mi_variable datetime;
select now() into mi_variable;
answered by 07.09.2018 в 22:56