设置存储过程,以便用户可以使用任何日期调用它。我希望能够将日期限制为两个月。
现在,当用户在没有选择任何日期的情况下调用报表时,报表服务器崩溃,因为数据太多。如果过去的日期超过2个月,我希望能够限制行数。
DELIMITER ;;
DROP PROCEDURE IF EXISTS rpt_missing_payroll_files_report_test;;
CREATE PROCEDURE `rpt_missing_payroll_files_report_test`(
IN BEGIN_DATE VARCHAR(255),
IN END_DATE VARCHAR(255)
)
BEGIN
SELECT c.id,
c.name,
ip.name AS icp_name,
s.name AS country_name,
sc.name AS pm_name,
pc.frequency,
pc.check_date AS check_date,
pc.transmit_date,
IFNULL(ps.updated_at, ph.updated_at) AS submit_date,
cpp.created_at AS date_received,
ifnull(cpp.import_filename,'N/A') AS import_filename,
CASE WHEN cpp.created_at IS NOT NULL THEN 1 END AS date_received_sum,
CASE WHEN cpp.success = 1 THEN 1 END AS file_successsum,
DATEDIFF(pc.check_date,cpp.created_at) AS date_diff,
DATE_FORMAT(BEGIN_DATE,'%m/%d/%Y') AS BeginDate,
DATE_FORMAT(END_DATE,'%m/%d/%Y') AS EndDate
FROM co_payroll_calendars pc
inner join co_infos c on pc.co_info_id = c.id
inner join icp_countries icp on c.icp_country_id = icp.id
INNER JOIN sys_countries s ON icp.sys_country_id = s.id
INNER JOIN icp_infos ip ON ip.id = icp.icp_info_id
LEFT OUTER JOIN co_payroll_entry_setups ps ON pc.id = ps.co_payroll_calendar_id
LEFT OUTER JOIN co_payroll_entry_setup_histories ph ON pc.id = ph.co_payroll_calendar_id
LEFT OUTER JOIN co_payroll_processes cpp ON cpp.check_date >= (pc.check_date - INTERVAL 10 DAY ) AND cpp.co_info_id = c.id
LEFT OUTER JOIN sys_csrs sc ON c.sys_csr_id = sc.id
-- below is the part i probably need to change
WHERE (ifnull(BEGIN_DATE,'') = ''
OR pc.check_date >= BEGIN_DATE)
AND (ifnull(END_DATE,'') = ''
OR pc.check_date <= END_DATE)
GROUP BY id,
check_date
ORDER BY country_name, check_date DESC, c.id;
END;;
DELIMITER ;
应该允许以下
CALL rpt_missing_payroll_files_report_test(&#39; 2017-06-01&#39;,&#39; 2017-07-01&#39;)
以下内容不应被允许或限于一定数量的行
CALL rpt_missing_payroll_files_report_test(&#39; 2016-01-01&#39;,&#39; 2017-07-01&#39;)
答案 0 :(得分:0)
将参数设置为正确的类型(即使用DATE,而不是VARCHAR)。
您还应该检查一下:
LEFT OUTER JOIN co_payroll_processes cpp
ON cpp.check_date >= (pc.check_date - INTERVAL 10 DAY )
AND cpp.co_info_id = c.id
请注意,不等式将选择具有匹配id的所有行,其日期在(pc.check_date - INTERVAL 10 DAY)之后。这可能很多,也可能不是,具体取决于您的架构。您应该在没有GROUP BY和ORDER BY的情况下测试查询,并检查它返回的内容。如果你发现很多重复项,那么你可以通过使用更好的JOIN条件使查询更快。
现在,关于你的主要问题,答案很简单:将此添加到你的proc
IF END_DATE > (BEGIN_DATE + INTERVAL 2 MONTH) THEN
raise an error
ELSE
your huge SELECT
END IF
这很简单。以下是如何在mysql存储过程中raise an error(这样做取决于你的版本)。
你也可以简单地在这里“选择你的消息”,然后消息字符串将返回给客户端(然后可能会崩溃,因为它需要一组特定的列,所以提出错误会更好)。