来自触发器的MySQL过程调用始终为输出参数返回null

问题描述 投票:0回答:1

我的存储过程OUT参数,始终返回一个空值。

这里是示例表,触发器和过程代码。

表格:test列:

  • id - Int
  • status - enum(‘pass’, ‘fail’)(允许为空)

表中的值:

id  |  status
1   |  null

触发器:

create trigger BEFORE_UPDATE_TEST before update on `test` for each row begin

    call Test_BEFORE_UPDATE_TEST(old.id, @updatedStatus);

       ## I always get @updatedStatus null/nil

    if (@updatedStatus is not null and @updatedStatus <> new.status) then
        set new.status = @updatedStatus;
    end if;

end;

步骤:

create procedure Test_BEFORE_UPDATE_TEST (
  IN id int(5),
  OUT status enum(‘pass’, ‘fail’)
)
begin
   @status = ‘pass’;

END;

此代码有什么问题,因为我在@updatedStatus值(应为'pass')中得到null的结果,但结果出乎意料。

我环顾四周关于stackoverflow的QA,但找不到解决方案。

我在MacOS Catalina中使用MySQLWorkbench,而MySQL的版本是8.0.19。

mysql stored-procedures triggers procedure out-parameters
1个回答
0
投票

该过程中的OUT status参数不同于用户定义的变量@status。这是总共两种不同的变量类型。

因此,该过程应类似于:

create procedure Test_BEFORE_UPDATE_TEST (
IN id int(5),
OUT status enum('pass', 'fail')
)
begin
 set status = 'pass';
END;

在触发器中,您还应该使用用DECLARE声明的普通变量:

create trigger BEFORE_UPDATE_TEST 
before update on test for each row 
begin

declare v_status enum('pass', 'fail');

call Test_BEFORE_UPDATE_TEST(old.id, v_status);

if (v_status is not null and v_status <> new.status) then
  set new.status = v_status;
end if;

end;
© www.soinside.com 2019 - 2024. All rights reserved.