IN日期的MySQL过程失败

时间:2011-04-08 21:16:39

标签: mysql stored-procedures

第一次海报非常感谢任何帮助。

我完全陷入了MySQL存储过程的问题,而且完全是绿色的。

这是当前编写的过程,MySQL接受它没有任何问题,所以除非语法错误,否则程序写得正确。

然而,当我打电话给程序时 call test (2011-04-01, 2011-04-07);  没有返回任何结果,但select语句运行正常。

CREATE PROCEDURE `NewProc`(IN `@StartDate` date,IN `@EndDate` date)

BEGIN

SELECT aux1, aux2, aux3, aux4, date, id, type,
    CASE
        WHEN results = 'pass' THEN '1'
        WHEN results = 'fail' THEN '0'
        ELSE '-1'
    END AS CertStatus,
    CASE
        WHEN results = 'pass' THEN '1'
        WHEN results = 'fail' THEN '0'
        ELSE '-1'
    END AS TestStatus,
    CASE
        WHEN results = 'pass' THEN '1'
        WHEN results = 'fail' THEN '0'
        ELSE '-1'
    END AS TestStatus,
    CASE
        WHEN results = 'pass' THEN '1'
        WHEN results = 'fail' THEN '0'
        ELSE '-1'
    END AS TestStatus
from completed
WHERE date >= '@StartDate' and date <= '@EndDate';
END;

3 个答案:

答案 0 :(得分:2)

快速整理 - 我会把它留给'@You'来点'差异'而且还有超过'1'

delimiter ;

drop procedure if exists list_test_status;

delimiter #

create procedure list_test_status
(
in p_start_date date,
in p_end_date date
)
begin

select aux1, aux2, aux3, aux4, date, id, type,
case
    when results = 'pass' then 1
    when results = 'fail' then 0
    else -1
end as CertStatus,
case
    when results = 'pass' then 1
    when results = 'fail' then 0
    else -1
end as TestStatus,
case
    when results = 'pass' then 1
    when results = 'fail' then 0
    else -1
end as TestStatus,
case
    when results = 'pass' then 1
    when results = 'fail' then 0
    else -1
end as TestStatus
from 
 completed
where 
 date >= p_start_date and date <= p_end_date;

end#

delimiter ;

call list_test_status(curdate() - interval 1 month, curdate());

答案 1 :(得分:0)

日期必须在引号内传递

call test ('2011-04-01', '2011-04-07');

修改。如果存储过程名为NewProc

,为什么要调用test

答案 2 :(得分:0)

不要引用变量,否则它是无效的日期(字符串'@xxx'无法转换为日期)。
不要反推参数
你也回来了3次TestStatus ......疯狂的东西!

CREATE PROCEDURE `NewProc`(IN StartDate date,IN EndDate date)
BEGIN

SELECT aux1, aux2, aux3, aux4, date, id, type,
    CASE
        WHEN results = 'pass' THEN '1'
        WHEN results = 'fail' THEN '0'
        ELSE '-1'
    END AS CertStatus,
    @cache := CASE
        WHEN results = 'pass' THEN '1'
        WHEN results = 'fail' THEN '0'
        ELSE '-1'
    END AS TestStatus,
    @cache AS TestStatus, # 2nd time ?
    @cache AS TestStatus  # 3rd time ??
from completed
WHERE date >= StartDate and date <= EndDate;
END;