我在 MySQL 中编写了下面提到的过程。
CREATE DEFINER=`root`@`localhost` PROCEDURE `IsUploaderLoggedIn`(
IN `inMobile` CHAR(10),
IN `inSessionID` varchar(34)
)
BEGIN
DECLARE isLoggedIn TINYINT(1) DEFAULT 0;
DECLARE uploaderType VARCHAR(10) DEFAULT '';
CALL GetUploaderType(inMobile, @x);
SELECT @x INTO uploaderType;
IF uploaderType = "surveyor" THEN
SELECT Count(*) INTO isLoggedIn FROM surveyors WHERE Mobile = inMobile AND SessionID = inSessionID;
SELECT "surveyor";
ELSE
SELECT Count(*) INTO isLoggedIn FROM uploaders WHERE Mobile = inMobile AND SessionID = inSessionID;
SELECT "uploader";
END IF;
SELECT isLoggedIn;
END;
在 Navicat 中执行该过程时,对于给定的 inMobile
和 inSessionID
值,返回的值为:
结果 1:用户类型 |测量员
结果 1(2):上传者 |上传者
结果 1(3):0 或 1(视情况而定)
其中,结果 1(2)s 的值应该是 surveyor |测量员。
下面是存储过程GetUploaderType
的定义:
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetUploaderType`(
IN `inMobile` CHAR(10),
OUT `Usertype` VARCHAR(8)
)
BEGIN
DECLARE usertype VARCHAR(8) DEFAULT '';
DECLARE userExists TINYINT DEFAULT 0;
/*SET officeExists = 0;*/
/* Check if mobile number belongs to a surveyor */
SELECT Count(*) INTO userExists FROM surveyors WHERE Mobile = inMobile;
IF userExists = 1 THEN
SET usertype = 'surveyor';
ELSE
/* If user is not surveyor, check for it in uploaders */
SELECT Count(*) INTO userExists FROM uploaders WHERE Mobile = inMobile;
IF userExists = 1 THEN
SET usertype = 'uploader';
END IF;
END IF;
SELECT usertype;
结束
请问我的脚本有什么问题吗?
最佳答案
在 GetUploaderType
中,您重新声明了变量 Usertype
,从而隐藏了同名的(不同的)out
变量,请参阅 scope :
The scope of a local variable is the BEGIN ... END block within which it is declared. The variable can be referred to in blocks nested within the declaring block, except those blocks that declare a variable with the same name.
关于mysql - 如果 MySQL 存储过程中的条件未正确评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51991708/