2013-04-23 76 views
6

我不想返回結果集,我不知道我在這裏做錯了什麼。 MySQL 5.5MySQL函數 - 錯誤代碼:1415不允許從函數返回結果集

delimiter $$ 

CREATE FUNCTION CheckAccount(
    i_username varchar(50) 
) RETURNS integer 

BEGIN 

    DECLARE v_validUserId int; 
    DECLARE v_validMembership int; 
    DECLARE o_Status integer; 

    SELECT vvalidUserId = u.UserId 
    FROM Users u 
    WHERE u.Username = i_username; 

    IF(v_validUserId IS NULL) THEN 
     SET o_Status = 2; -- Invalid username 
    ELSE 
     SET o_Status = 1; -- Good 
    END IF; 


    IF(o_Status != 2) THEN 
      SELECT v_validMembership = 1 
      FROM Users u 
      JOIN UserMemberships um on um.UserId = u.userId 
      JOIN Memberships m on m.MembershipId = um.MembershipId 
      WHERE um.MembershipExpireDateTime > CURDATE() 
      AND u.UserId = v_validUserId; 

      IF(v_validMembership IS NULL) THEN 
       SET o_Status = 3; -- Invalid membership 
      END IF; 
    END IF; 

    RETURN o_status; 

END $$ 
DELIMITER ; 

任何幫助將不勝感激!

回答

8

我不確定您是否可以用這種方式分配變量,請嘗試使用INTO語句作爲您的選擇。例如:

SELECT 
    u.UserId INTO vvalidUserId 
FROM 
    Users u 
WHERE 
    u.Username = i_username; 
+0

你是天才!非常感謝!這並沒有表明這是一個錯誤,所以我從來沒有發現這一點。 – sqltracy 2013-04-24 14:50:10