我试图使用此程序:
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
但我收到以下错误:ERROR 1305(42000):FUNCTION db.get_usertype不存在。
CALL db.get_usertype( (SELECT ID FROM User WHERE Email = 'example@example.com') );
当我单独测试时,工作正常。任何人都知道它为什么起作用?
我试过了:
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!
答案 0 :(得分:2)
编辑完全忽略我之前关于在存储过程中调用存储过程的评论!存储过程不返回结果。但是,您可以将存储过程插入临时表中,您可以从中进行选择。以下答案仍然是您所寻求的。
CALL db.get_usertype((SELECT ID FROM User WHERE Email = in_Email ));
SET d_UserType = (SELECT usertype FROM temp_get_usertype);
我相信你想要的是get_usertype
而不是function。在某些情况下,您可以使用视图。
SET d_UserType = db.get_usertype((SELECT ID FROM User WHERE Email = in_Email ))
e.g。
CREATE FUNCTION `get_usertype`(userID INT) RETURNS INT
BEGIN
DECLARE $type INT;
...
RETURN $type;
END