2012-09-19 51 views
0

我正在編寫一個過程來顯示當前學生是否在表中。測試之後,我收到了幾個錯誤的...MySQL過程案例不會返回null

Create procedure stuID(p_id int)  
begin 
    declare v_msg varchar(100); 
    declare count_id int; 

    select count(*) into count_id from students where id = p_id; 


    If null then 
     set v_msg := 'null'; 
    Elseif count_cl_id = 1 then 
     set v_msg := 1; 
    Elseif count_cl_id = 0 then 
     set v_msg := 0; 
    End if; 

    Select v_msg; 
end; 

於是我創造了另一個程序來測試這些...

CREATE PROCEDURE test_stuID() 開始

-- Test with null 
    call stuID(null); 

    -- Test with invalid ID 
    call stuID('0'); 

    -- Test with String 
    call stuID('false'); 

    -- Test with true ID 
    call stuID('25'); 

end; 

兩個問題:

1)空的情況下不返回v_msg爲「空」

2)真實有效的身份證件25沒有得到恢復。

我在做什麼錯?提前致謝。

回答

0

2個錯誤:

1)If nullif count_id is null

2)您使用count_cl_id比較而count_id是帕拉姆名稱所取代。

我在我的系統上測試了更正,並且工作正常。

+0

hm。我看到了這一點,並嘗試了它(因爲它是有道理的)。但它不起作用。然後我輸入:call stuID(null); 並將v_msg設置爲0,而不是null。思考? –

+0

我編輯了我的答案請檢查它,並讓我知道它是否有幫助 – Satya

+0

對不起,一直在問,你得到一個null作爲輸出?或0?當我將v_msg設置爲null來表示null情況時,我一直得到0。謝謝 –