MySQL存储过程始终选择True
我有这样一个存储过程:MySQL存储过程始终选择True,mysql,stored-procedures,Mysql,Stored Procedures,我有这样一个存储过程: delimiter // create procedure UserRole_IsUserAdmin(userID int) begin if userID is null then select 'User ID is required.'; else if ( select count(UserRoleCode)
delimiter //
create procedure UserRole_IsUserAdmin(userID int)
begin
if userID is null then
select 'User ID is required.';
else
if (
select
count(UserRoleCode)
from
UserRole
where
UserID = userID
and RoleCode = 1
and VendorCode is null
and NPOCode is null
) > 0 then
select true;
else
select false;
end if;
end if;
end;
//
if语句中的子查询有时返回0(例如,当userID为5时),有时返回1(例如,当userID为1时)。问题是存储过程总是选择true。我不知道为什么,这让我发疯。我不知道我在if声明中是否做错了什么
调用该过程的代码:
call UserRole_IsUserAdmin(5);
/*Should return false*/
call UserRole_IsUserAdmin(1);
/*Should return true*/
根据要求,这是表格的数据
UserID是用户的FK ID,RoleCode是用户所在角色的FK ID。UserRoleCode只是一个自动递增的PK。如果存在ncode和vcode为null且rolecode为1的记录,则该过程应返回true。我没有您的简单数据,但对我来说,是这样工作的
BEGIN
IF userID IS NULL THEN
SELECT 'User ID is required.';
ELSE
SELECT IF(COUNT(*)>0,TRUE,FALSE) AS if_stmt
from
UserRole
where
UserID = userID
and RoleCode = 1
and VendorCode is null
and NPOCode is null;
END IF;
END$$
最好将其设置为返回true或false的函数。三态“return”值有点尴尬,尽管它看起来应该可以工作。也许调用它的代码是个问题?让我们看看。刚刚更新了问题,抱歉好吧,你已经添加了呼叫,但是你如何知道它返回的是false还是true?代码在哪里。我现在正在使用MySQL工作台。@Jack你看到ny的答案了吗,错了吗?