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

  • Home
  • SEARCH
  • 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 907955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:38:14+00:00 2026-05-15T16:38:14+00:00

I have the following bunch of code trying to update a field using 2

  • 0

I have the following bunch of code trying to update a field using 2 cursors the one inside the other. I use the first cursor just to get an id value (1st cursor) and then I get a list of other id values based on that id (2nd cursor). The problem is that the result set from the 2nd cursor contains the last id twice! I can’t find the bug! Something in the loop, something in the termination variable for the 2 cursors?

Any help more than welcome! Thanks in advance!

DELIMITER $$
DROP PROCEDURE IF EXISTS updateNumberOfPoisForPlacesWithChildren$$
CREATE PROCEDURE updateNumberOfPoisForPlacesWithChildren()
BEGIN
DECLARE single_place_id INT(11);
DECLARE single_unique_parents_id INT(11);
DECLARE done, temp_number_of_pois, sum_number_of_pois INT DEFAULT 0;

    DECLARE unique_parents_ids CURSOR FOR SELECT DISTINCT `parent_id` FROM `places` WHERE `parent_id` IS NOT NULL;
    DECLARE temp_places_ids CURSOR FOR SELECT `id` FROM `places` WHERE `parent_id` = single_unique_parents_id;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN unique_parents_ids;

    main_loop_for_parent_ids : LOOP
        SET sum_number_of_pois = 0;
        IF done = 1 THEN
            CLOSE unique_parents_ids;
            LEAVE main_loop_for_parent_ids;
        END IF;

        IF NOT done = 1 THEN
            FETCH unique_parents_ids INTO single_unique_parents_id;
            OPEN temp_places_ids;
            main_loop_for_places : LOOP
                IF done = 1 THEN
                    CLOSE temp_places_ids;
                    LEAVE main_loop_for_places;
                END IF;

                IF NOT done = 1 THEN
                    FETCH temp_places_ids INTO single_place_id;
                    SELECT COUNT(`id`) INTO temp_number_of_pois FROM `pois` WHERE `place_id` = single_place_id;
                    SET sum_number_of_pois = sum_number_of_pois + temp_number_of_pois;
                END IF;

            END LOOP;   

            UPDATE `places` SET `number_of_pois`=sum_number_of_pois WHERE `id` = single_unique_parents_id;

        END IF;
    END LOOP;



END$$

DELIMITER ;

  • 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-15T16:38:15+00:00Added an answer on May 15, 2026 at 4:38 pm

    I think your problem is about here…

            IF NOT done = 1 THEN
                FETCH temp_places_ids INTO single_place_id;
                SELECT COUNT(`id`) INTO temp_number_of_pois FROM `pois` WHERE `place_id` = single_place_id;
                SET sum_number_of_pois = sum_number_of_pois + temp_number_of_pois;
            END IF;
    

    The FETCH sets DONE to be 1 when it exhausts the result set, but you proceed because you’ve already passed the DONE test.

    Perhaps…

     FETCH temp_places_ids INTO single_place_id;
     IF NOT done = 1 THEN
         SELECT COUNT(`id`) INTO temp_number_of_pois FROM `pois` WHERE `place_id` = single_place_id;
         SET sum_number_of_pois = sum_number_of_pois + temp_number_of_pois;
     END IF;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge bunch of XML files with the following structure: <Stuff1> <Content>someContent</name>
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
Possible Duplicate: C# Update Table using SqlCommand.Parameters I'm trying to update an SQL Server
I'm trying to write a piece of code that will do the following: Take
So, I have a bunch of code that I'm pulling from a column in
I have the following code in a stand-alone Cocoa test app: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

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.