Filtrado MySQL en posible valor nulo

i am writing a mysql stored procedure that takes a single datetime parameter. it uses it in a select statement as such..

select *
from table
where date >= @datefrom or @datefrom is null

when i write t-sql queries this usually does the trick but it doesnt seem to be working with mysql.

i have also tried other variants such as

where date >= ifnull(@datefrom, date)

y también

where ((date >= @datefrom and @datefrom is not null) or (@datefrom is null))

but im not getting the results i am expecting..

can someone shed any light on this?

procedure definition as such..

CREATE DEFINER=`root`@`%` PROCEDURE `prc_xxxxxx`(datefrom datetime)

select * from table
where date >= @datefrom or @datefrom is null


and then i would call like this..

call prc_xxxxxx ('2012-07-04 00:00:00')


call prc_xxxxx (null)

preguntado el 04 de julio de 12 a las 08:07

Are you running this query as it appears here? with the @ in the query? -

Prueba el NULL-operador de igualdad seguro <=>. -

@ is used for session variables. Has the value of @param been set earlier in the same session? -

I mean that you should use procedure paramater instead of variable. -

Please show your procedure definition. -

1 Respuestas

As already mentioned, the @ symbol is used for variables de sesión, not for stored routine parameters—those should go unprefixed. E.g.:

    SELECT var1 + 2 AS result;  

Este ejemplo fue tomado de este tutorial.

Respondido 04 Jul 12, 10:07

oh - so we doint use '@' like in T-SQL ?? - Grant

Neither MySQL nor Oracle use @ for stored routine parameters. - Álvaro González

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.