Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:41:49+00:00 2026-05-30T09:41:49+00:00

I am not the most knowledge able person when it comes to stored procedures,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:41:50+00:00Added an answer on May 30, 2026 at 9:41 am

    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'; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this c# code is probably not the most efficient but gets what I want
This is most likely not an easy one but here is the situation: I
I am trying to find a universal way to expand most if not all
I am not the most experienced GUI programmer, so bear with me here. I
HashTables/HashMaps are one of the most (if not the most) useful of data-structures in
I've played with CVS a little bit and am not the most familiar with
Whilst asynchronous IO (non-blocking descriptors with select/poll/epoll/kqueue etc) is not the most documented thing
I know I am not running the most up-to-date version of ACT on my
In most examples that you find on the web when explicitly not using using,
(Not really a programming question, sorry) I'm working on benchmarking various filesystems (most importantly:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.