我对存储过程感到满意,以下让我感到困惑。 似乎@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 $$
答案 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