我正在编写一个mysql存储过程,它只接受一个datetime参数。它在select语句中使用它。。
select *
from table
where date >= @datefrom or @datefrom is null
当我编写t-sql查询时,这通常起到了作用,但它似乎不适用于mysql。
我也尝试过其他变体,比如
where date >= ifnull(@datefrom, date)
还有
where ((date >= @datefrom and @datefrom is not null) or (@datefrom is null))
但我没有得到我期待的结果。。
有人能解释一下吗?
程序定义。。
CREATE DEFINER=`root`@`%` PROCEDURE `prc_xxxxxx`(datefrom datetime)
BEGIN
select * from table
where date >= @datefrom or @datefrom is null
END
然后我就这样打电话。。
call prc_xxxxxx ('2012-07-04 00:00:00')
或
call prc_xxxxx (null)
最佳答案
如前所述,@symbol用于session variables,而不是应取消固定的存储例程参数。例如。:
CREATE PROCEDURE `proc_IN` (IN var1 INT)
BEGIN
SELECT var1 + 2 AS result;
END//
这个例子取自this tutorial。