2011-11-06 66 views
2

我試着去使用這個程序:MySQL的:調用存儲過程內部程序與返回值

DELIMITER $$ 

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_login`(
    IN in_Email VARCHAR(60), 
    IN in_Pass VARCHAR(45) 
    ) 
BEGIN 
    DECLARE d_UserType VARCHAR(45); 
    SET d_UserType := db.get_usertype((SELECT ID FROM User WHERE Email = in_Email)); 

    IF (d_UserType = '1') THEN 

     SELECT * FROM User inner join tableA on tableA.ID = User.tableA_ID; 

    ELSEIF (d_UserType = '2') THEN 

     SELECT * FROM User inner join tableB on tableB.ID = User.tableB_ID; 

    ELSEIF (d_UserType = '3') THEN 

     SELECT * FROM User inner join tableC on tableC.ID = User.tableC_ID; 

    END IF; 
END 

,但我得到以下錯誤:錯誤1305(42000):功能db.get_usertype不存在。

CALL db.get_usertype((SELECT ID FROM User WHERE Email = '[email protected]'));   

當我獨自一人測試工作正常。任何人都知道它爲什麼起作用?

我用盡:

SET d_UserType := CALL db.get_usertype((SELECT ID FROM User WHERE Email = in_Email)); 
SET d_UserType := EXEC db.get_usertype((SELECT ID FROM User WHERE Email = in_Email)); 
SET d_UserType := EXECUTE db.get_usertype((SELECT ID FROM User WHERE Email = in_Email)); 

沒有運氣..

THX的所有幫助!

回答

2

編輯完全忽略我之前關於調用存儲過程中存儲過程的註釋!存儲過程不會返回結果。但是,您可以將存儲過程插入臨時表中,您可以從中選擇。以下答案仍然是你所尋求的。

CALL db.get_usertype((SELECT ID FROM User WHERE Email = in_Email)); 
SET d_UserType = (SELECT usertype FROM temp_get_usertype); 

我相信你想要的是讓get_usertypefunction代替。在某些情況下,您可以使用視圖。

SET d_UserType = db.get_usertype((SELECT ID FROM User WHERE Email = in_Email)) 

例如,

CREATE FUNCTION `get_usertype`(userID INT) RETURNS INT 
BEGIN 
    DECLARE $type INT; 
    ... 
    RETURN $type; 
END