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 8127545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:34:07+00:00 2026-06-06T07:34:07+00:00

Hi I have a nested cursor with three levels on it… DECLARE campaign_csr CURSOR

  • 0

Hi I have a nested cursor with three levels on it…

        DECLARE campaign_csr CURSOR FOR SELECT cid FROM tempCampaign; 
        DECLARE date_csr CURSOR FOR SELECT header,ranges FROM tempDate ORDER BY id; 
        DECLARE level_csr CURSOR FOR SELECT 1st,2nd,3rd FROM tempDispoLevels ORDER BY 1st,2nd; 
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done := TRUE; 
      OPEN campaign_csr; 
           campaign_loop:LOOP 
           FETCH campaign_csr INTO xcid; 
            IF v_done THEN 
                 CLOSE campaign_csr; 
                 LEAVE campaign_loop; 
             END IF; 
select "1";
            OPEN date_csr; 
                date_loop: LOOP 
                FETCH date_csr INTO xheader,xranges; 
                 IF v_done THEN 
                 SET v_done := FALSE;
                     CLOSE date_csr; 
                    LEAVE date_loop; 
                END IF; 
select "2";
                       OPEN level_csr; 
                        level_loop: LOOP 
                        FETCH level_csr INTO x1level,x2level,x3level; 
                         IF v_done THEN 
                             SET v_done := FALSE; 
                             CLOSE level_csr; 
                            LEAVE level_loop; 
                         END IF;
select "3";
              END LOOP level_loop;
       END LOOP date_loop;
  END LOOP campaign_loop;

the problem is the outer loop (which in this case campaign_loop) doesn’t read all records. Is there any way around this.
Thank you.

  • 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-06-06T07:34:10+00:00Added an answer on June 6, 2026 at 7:34 am

    I discover a work around on this: The added variables are for looping the outside cursor until to the last row.

    1. Declare 3 more v_done variable

      DECLARE v_done BOOLEAN DEFAULT FALSE; //this is the original
      DECLARE v_done1 BOOLEAN DEFAULT FALSE; //added
      DECLARE v_done2 BOOLEAN DEFAULT FALSE; //added
      DECLARE v_done3 BOOLEAN DEFAULT FALSE;//added
      
    2. Keep this line:

      DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done := TRUE;

    3. Change every declaration of cursor:

      OPEN campaign_csr;
      campaign_loop:LOOP
      FETCH campaign_csr INTO xcid; 
            IF v_done THEN 
                 CLOSE campaign_csr; 
                 LEAVE campaign_loop; 
             END IF;
    

    into:

    OPEN campaign_csr; 
               campaign_loop:LOOP 
               FETCH campaign_csr INTO xcid; 
               SET v_done1 := v_done;
                IF v_done1 THEN 
                SET v_done := FALSE; 
                     CLOSE campaign_csr; 
                     LEAVE campaign_loop; 
                 END IF;
    

    This code works for me.

    DELIMITER $$
    
    USE `yourDatabase`$$
    
    DROP PROCEDURE IF EXISTS `yourProcedure`$$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `yourProcedure`()
    BEGIN 
            DECLARE xcid INT;
            DECLARE xcid2 INT;
            DECLARE xcid3 INT;
            DECLARE v_done BOOLEAN DEFAULT FALSE; 
            DECLARE v_done1 BOOLEAN DEFAULT FALSE; 
            DECLARE v_done2 BOOLEAN DEFAULT FALSE; 
            DECLARE v_done3 BOOLEAN DEFAULT FALSE; 
            DECLARE campaign_csr CURSOR FOR SELECT column FROM yourTable; 
            DECLARE date_csr CURSOR FOR SELECT column FROM yourTable2; 
            DECLARE level_csr CURSOR FOR SELECT column FROM yourTable3; 
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done := TRUE; 
    
            OPEN campaign_csr; 
                 campaign_loop:LOOP 
                 FETCH campaign_csr INTO xcid; 
                 SET v_done1 := v_done;
                 IF v_done1 THEN 
                    SET v_done := FALSE; 
                    CLOSE campaign_csr; 
                    LEAVE campaign_loop; 
                 END IF; 
    
                    select "1";
    
                 OPEN date_csr; 
                      date_loop: LOOP 
                      FETCH date_csr INTO xcid2; 
                  SET v_done2 := v_done;
                  IF v_done2 THEN 
                     SET v_done := FALSE; 
                         CLOSE date_csr; 
                         LEAVE date_loop; 
                      END IF;
    
                      select "2";
    
                      OPEN level_csr; 
                           level_loop: LOOP 
                           FETCH level_csr INTO xcid2; 
                       SET v_done3 := v_done;
                       IF v_done3 THEN 
                       SET v_done := FALSE; 
                                   CLOSE level_csr; 
                                   LEAVE level_loop; 
                               END IF; 
    
                      select "3";
    
                      END LOOP level_loop;
                  END LOOP date_loop;
             END LOOP campaign_loop;
    
    END$$
    
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have nested xsl:for loops: <xsl:for-each select=/Root/A> <xsl:for-each select=/Root/B> <!-- Code --> </xsl:for> </xsl:for>
I have created nested cursor which basically compares column values of one table To
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have nested iframes and I would like to use onload function for the
What if I have nested loops, and I want to break out of all
Is it possible to have nested set capabilities in this somewhat custom setup? Consider
I have a situation where I have nested toggle statements in jQuery. I have
I have a number of tables, which have nested tables. I using jQuery to
I'm using colorbox on a div. I have nested div's. When i click the
I'm working on a navigation control. I have nested lists which creates this: What

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.