gpt4 book ai didi

mysql - 存储过程不会返回 OUT var

转载 作者:行者123 更新时间:2023-11-29 11:53:33 25 4
gpt4 key购买 nike

我对存储过程很感兴趣,下面的内容让我感到困惑。看来 @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 $$

最佳答案

架构

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

关于mysql - 存储过程不会返回 OUT var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com