0

I'm using Laravel 6.2 and I have the following code in my controller

$date_ini='01-01-2022';
$date_fin='31-12-2022';

$seg=\DB::select('CALL pr_SegOrder(?,?,?)',[$client,$date_ini,$date_fin]);

return $seg;

And this is my stored procedure:

CREATE DEFINER=`root`@`localhost` PROCEDURE `pr_SegOrder`(`client` varchar(200), `date_ini` datetime, `date_fin` datetime)
BEGIN
 SELECT cod,norder 
FROM
  order op 
WHERE
cast(op.created_at as date) BETWEEN @date_ini AND @date_fin 

ORDER BY
    op.cod asc ;
END

If I execute my query on MySql everything works fine. When I execute the SP from Laravel there is no error message, just an empty result.

The problem only ocurring when i use date parameters, because if only send "client" parameter from laravel, works fine.

any idea what could occurring?

1 Answer 1

3

In MySQL, procedure input arguments like date_ini do not use the @ sigil. That character is for user-defined session variables. They are not the same variable.

When you used the expression BETWEEN @date_ini AND @date_fin, those two variables probably have the value NULL, unless you had set their value in the session before calling the procedure.

So just use BETWEEN date_ini AND date_fin, without the @ characters. Then they will be references to the procedure input arguments.

Be careful not to name your procedure arguments the same as columns in the tables, or else your query will refer to the columns, not the variables.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, works fine do not using @ sigil. But I've the same problem when i pase Year argument to stored procedure. CREATE DEFINER PROCEDURE pr_SegOrder(date_fin datetime, time_year INTEGER)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.