我有一个存储过程,它接收6个变量,这些变量可能为空,也可能不为null,具体取决于我在web项目前端应用的过滤器。但是,即使所有过滤器都设置为null,当前此存储过程也不会返回任何内容。我正在使用MySQL工作台,如果这有所作为。以前我一直在使用单独的存储过程,具体取决于应用的过滤器,但由于我添加了过滤器,因此管理起来太难了。
CREATE DEFINER=`root`@`localhost` PROCEDURE `spFiltersGet`(IN jAlertId int, IN timeFrom timestamp, IN timeTo timestamp, IN seconds int, IN alertStatus varchar(255), IN jHostName varchar(255))
BEGIN
DECLARE timeFromVal TIMESTAMP;
DECLARE timeToVal TIMESTAMP;
DECLARE fetchVal INT;
DECLARE offsetVal INT;
DECLARE alertIdVal INT;
DECLARE secondsVal INT;
DECLARE statusVal varchar(255);
DECLARE hostVal varchar(255);
SET timeFromVal = timeFrom;
SET timeToVal = timeTo;
SET fetchVal = 10;
SET offsetVal = 0;
SET alertIdVal = jAlertId;
SET secondsVal = seconds;
SET statusVal = alertStatus;
SET hostVal = jHostName;
SELECT * FROM job_alert
WHERE
(alertIdVal IS NULL OR alert_id = jAlertId)
AND (timeFromVal IS NULL && timeToVal IS NULL
OR start_time BETWEEN timeFromVal AND timeToVal)
AND (secondsVal IS NULL
OR time_diff >= seconds)
AND (statusVal IS NULL
OR status = alertStatus)
AND (hostVal IS NULL OR host_name = jHostName)
ORDER BY alert_id DESC
LIMIT OFFSETVAL , FETCHVAL;
END
如果没有应用过滤器,但都设置为null,我将它转换为JSON后,我期待这种输出:
{
error: "null",
jobStatus: "SUCCESS",
startTime: "Sep 9, 2016 10:42:54 AM",
endTime: "Sep 9, 2016 10:42:55 AM",
timeDiff: 1217,
runCount: 2,
jobInstanceId: 834,
host: "IT2561",
nextRun: "Sep 9, 2016 10:52:54 AM",
jobCount: 11,
name: "Gift Certificate Delivery",
id: 6,
description: "Gift Certificate Delivery"
}
我是通过查看其他StackOverflow答案写的,所以我假设有一些我错过的东西。我的控制台中没有任何错误,它只返回没有记录。
答案 0 :(得分:0)
我试图修复它,没有任何数据很难编写查询。检查一下,告诉我是否有任何问题。我也删除了你的变量,你不需要它们:
CREATE PROCEDURE `spFiltersGet`(
IN jAlertId int, IN timeFrom timestamp, IN timeTo timestamp,
IN seconds int, IN alertStatus varchar(255), IN jHostName varchar(255))
BEGIN
DECLARE _fetchVal INT;
DECLARE _offsetVal INT;
SET _fetchVal = 10;
SET _offsetVal = 0;
SELECT
*
FROM
job_alert
WHERE
alert_id = IFNULL(jAlertId, alert_id) -- id
AND
((timeFrom IS NULL AND timeTo IS NULL) OR start_time BETWEEN timeFrom AND timeTo) -- start time
AND
(seconds IS NULL OR `time_diff` >= seconds) -- min time difference
AND
(alertStatus IS NULL OR `status` = alertStatus) -- status
AND
(jHostName IS NULL OR `host_name` = jHostName) -- host name
ORDER BY
alert_id DESC
LIMIT
_offsetVal, _fetchVal;
END