MySQL存储过程INSERT后SELECT var错误值

时间:2021-12-06 16:39:04

I have a problem on MySQL stored procedure, already did another logic with IF THEN ELSE, but I still have problems which I cant understand...

我有一个关于MySQL存储过程的问题,已经使用IF THEN ELSE做了另一个逻辑,但我仍然有一些我无法理解的问题......

The procedure consists on two user inputs: user_id and meet_id. The procedure define a variable called 'ue' which stores result of a bunch of validation (if user exists, if event exists, if event date is still valid, etc.).

该过程包含两个用户输入:user_id和meet_id。该过程定义了一个名为'ue'的变量,它存储了一堆验证的结果(如果用户存在,如果事件存在,如果事件日期仍然有效,等等)。

After that, it does INSERT and UPDATE some data on multiple tables in IF THEN ELSE selector, and SELECT 1 (or 0) AS result depending of validation.

之后,它会在IF THEN ELSE选择器中对多个表执行INSERT和UPDATE,并根据验证对SELECT 1(或0)AS结果进行更新。

But my problem is: it always return me 0 as 'result', as if my validation variable was 0 when I do INSERT... And there is where things get weird, if I remove the INSERT [...]; line of the code, it returns me the value of validation correctly (1 or 0).

但我的问题是:它总是将0作为'结果'返回0,就像我执行INSERT时我的验证变量为0 ...而且如果我删除了INSERT [...],那么事情会变得奇怪。代码行,它正确返回验证值(1或0)。

The code of the procedure is this one:

程序的代码是这样的:

CREATE DEFINER=`nwouserf`@`localhost` 
PROCEDURE `join_event`(IN `user_id` BIGINT(64), IN `event_id` INT) 
NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER 

begin 
DECLARE ue INT; 
SET ue = EXISTS(SELECT 1 FROM users WHERE fb_uid=user_id) 
         AND EXISTS(SELECT 1 FROM meetup WHERE meet_id=event_id) 
         AND EXISTS(SELECT 1 FROM meetup WHERE date > NOW() AND meet_id = event_id) 
         AND EXISTS(SELECT 1 FROM meetup WHERE meet_id = event_id AND participants <= max_participants) 
         AND NOT EXISTS(SELECT 1 FROM meetup_participation WHERE fb_uid = user_id AND meet_id = event_id); 

IF ue = 1 THEN 

    INSERT INTO meetup_participation (fb_uid, meet_id) VALUES (user_id, event_id); 
    UPDATE users SET events_participated = events_participated + 1 WHERE fb_uid=user_id; 
    UPDATE meetup SET participants = participants + 1 WHERE meet_id=event_id; 
    SELECT 1 AS result; 

ELSEIF ue = 0 THEN 
    SELECT 0 AS result; 
ELSE 
    SELECT null AS result; 
END IF; 
end

Thanks in advance! I have been stuck on this for a while now, and can not figure out why.

提前致谢!我现在已经坚持了一段时间,并且无法弄清楚原因。

1 个解决方案

#1


1  

You should define OUT parameter. Add

您应该定义OUT参数。加

", OUT result INT"

“,OUT结果INT”

immediately after the last IN parameter.

紧接在最后一个IN参数之后。

#1


1  

You should define OUT parameter. Add

您应该定义OUT参数。加

", OUT result INT"

“,OUT结果INT”

immediately after the last IN parameter.

紧接在最后一个IN参数之后。