存储过程不会返回OUT var

时间:2015-11-08 20:34:21

标签: mysql sql stored-procedures

我对存储过程感到满意,以下让我感到困惑。 似乎@Status变量没有被设置?

当我调用查询时,它运行正常,但返回null。 我正在使用:

call new_customer1('Someone@someemail.com', @Status);
SELECT @Status;

SP查询如下

CREATE PROCEDURE new_customer1(
IN CusEmail VARCHAR(75),
OUT Status TINYINT(2)
)
BEGIN
IF EXISTS (SELECT Email FROM customers WHERE Email = @CusEmail)
THEN SET @Status = 1;
ELSE
# Perform inssert
SET @Status = 0;
END IF;
END $$

1 个答案:

答案 0 :(得分:1)

模式

create table customers
(  id int auto_increment primary key,
   email varchar(100) not null
);

insert customers (email) values ('trump@trump.com');

存储过程

drop procedure if exists new_customer1;
DELIMITER $$
CREATE PROCEDURE new_customer1(IN CusEmail VARCHAR(75), OUT NewStatus TINYINT(2))
BEGIN
    IF EXISTS (SELECT Email FROM customers WHERE Email = CusEmail) THEN 
        SET NewStatus = 1;
    ELSE
        SET NewStatus = 0;
    END IF;
END
$$
DELIMITER ;

测试

call new_customer1('notthere',@theOut);
select @theOut; -- 0
call new_customer1('trump@trump.com',@theOut);
select @theOut; -- 1