Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL stored procedure select statement select incorrect ID

I am not the most knowledge able person when it comes to stored procedures, and this problem is blowing my mind!

Basically I am just trying to run a simple update statement, but the user's ID that I am selecting to update the row is not correct when I run it in the procedure, but if I run the same select statement outside the procedure it returns the expected results.

DELIMITER $$
CREATE PROCEDURE catchUpBbs_Users()
  BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE deleteUser, keepUser VARCHAR(255);
    DECLARE id INT;
    DECLARE cur1 CURSOR FOR SELECT u.username, b.username, b.id from users u RIGHT     JOIN bbs_users b ON u.username = b.username WHERE u.username IS NULL;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;     
    OPEN cur1;
        allusers: LOOP 
            FETCH cur1 INTO keepUser, deleteUser, id;
            IF done THEN 
                LEAVE allusers; 
            END IF;
IF deleteUser != 'anonymous' THEN 

-- This is where the problems start

-- Just for test purposes, returns correct id and username 
SELECT id, username FROM bbs_users WHERE username = deleteUser; 

-- Just for test purposes, return INCORRECT id, but the CORRECT username  
SELECT id, username FROM bbs_users WHERE username = 'anonymous'; 

-- The update statement that does not work as I want it to, sets the user_id to be what it already it, so no updates occur
UPDATE bbs_posts SET user_id = (SELECT id FROM bbs_users WHERE username = 'anonymous') WHERE user_id = (SELECT id FROM bbs_users WHERE username = deleteUser);

-- delete the user from the bbs_users table
DELETE FROM bbs_users WHERE username = deleteUser;

END IF;
    END LOOP allusers;
    CLOSE cur1;
END;

$$
DELIMITER ;

When I call the procedure the two test select statements return:

1) These results are both correct

 SELECT id, username FROM bbs_users WHERE username = deleteUser;
 +------+----------+
 | id   | username |
 +------+----------+
 |   13 | deleteme |
 +------+----------+

2) The ID is incorrect, but the username is incorrect, the ID should be 1

 SELECT id, username FROM bbs_users WHERE username = 'anonymous'; 
 +------+-----------+
 | id   | username  |
 +------+-----------+
 |   13 | anonymous |
 +------+-----------+

When I run the same, minus the variable, select statements outside the procedure both return the correct results

1) These results are both correct

SELECT id, username FROM bbs_users WHERE username = 'deleteme';
+----+----------+
| id | username |
+----+----------+
| 13 | deleteme |
+----+----------+

2) These results are both correct

SELECT id, username FROM bbs_users WHERE username = 'anonymous';
+----+-----------+
| id | username  |
+----+-----------+
|  1 | anonymous |
+----+-----------+

What am I doing wrong? Is there something that I missed when it comes to selects and variables when using a stored procedure?

Any help or advice would be much appreciated

like image 292
letsgettechnical Avatar asked Mar 21 '26 07:03

letsgettechnical


1 Answers

The issue is that you have a scalar variable called id defined in your cursor and you are selecting into it, so both of your select statements inside of the cursor are using that stored scalar value for all references to id.

In order to get the "correct" value, you'll need to remove all ambiguity:

-- Just for test purposes, returns correct id and username 
SELECT b.id, b.username FROM bbs_users b WHERE b.username = deleteUser; 

-- Just for test purposes, return INCORRECT id, but the CORRECT username  
SELECT b.id, b.username FROM bbs_users b WHERE b.username = 'anonymous'; 
like image 176
Brian Driscoll Avatar answered Mar 23 '26 02:03

Brian Driscoll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!