2011-04-08 40 views
1

第一次海報非常感謝任何幫助。帶日期輸入的MySQL程序失敗

我完全停留在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; 

回答

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()); 
+0

謝謝f00,這解決了我的問題。 – wolfsburg18 2011-04-12 18:13:49

0

日期必須引號

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

編輯內通過。如果您的存儲過程被命名爲NewProc,爲什麼要打電話測試?

+0

感謝您的快速回復,不幸的是,仍然沒有返回任何結果。 – wolfsburg18 2011-04-08 21:26:46

+0

您是否嘗試過在沒有存儲過程的情況下使用相同的兩個日期運行查詢?你確定這個範圍內有記錄嗎?此外,嘗試刪除@StartDate和@EndDate的報價 – 2011-04-08 21:31:10

+0

我甚至都不理解所有這些情況的含義。我以前沒有注意到它們。您始終將它們應用於相同的值並重復它們很多次。這沒用。 – 2011-04-08 21:43:28

0

不要引用變量,否則它是一個無效日期(字符串'@xxx'不能轉換爲日期)。
不要反顯參數
你也返回TestStatus 3次......瘋狂的東西!

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;