开发者

MySQL Procedure with IN date fails

开发者 https://www.devze.com 2023-02-23 07:28 出处:网络
First time poster really appreciate any assistance. I am completely stuck on an issue with MySQL stored procedures and completely green to them.

First time poster really appreciate any assistance.

I am completely stuck on an issue with MySQL stored procedures and completely green to them.

Here is the procedure as it is currently written, MySQL accepts it without any issue so unless there is a syntax error, the procedure is written correctly.

However when I call the procedure call test (2011-04-01, 2011-04-07); no results are returned, yet the select statement works just fine.

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 TestS开发者_StackOverflow社区tatus,
    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;


quick tidy up - i'll leave it to '@You' to SPOT the 'differences' and there are more than '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());


Dates must be passed within quotes

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

edit. Why do you call test if your stored procedure is named NewProc ?


Don't quote the variables, otherwise it is an invalid date (the string '@xxx' cannot convert to a date).
Don't backtick the parameters
You are also returning TestStatus 3 times... crazy stuff!

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消