当前位置:网站首页>[cursor nesting] nesting of MySQL stored procedure cursors

[cursor nesting] nesting of MySQL stored procedure cursors

2022-06-22 17:12:00 Seven days at night

We use mysql When storing a procedure Sometimes we go through a lot of data , So selecting a cursor is a good way It will help us get the data . But most of the time , We will need the combination of nested cursors to solve the problem of

problem . Okay, talk less , Let's start with a simple cursor :

// Define cycle markers

   DECLARE useraccountid INT DEFAULT 0; # The user id

 DECLARE done TINYINT(1) DEFAULT 0;

 // The account number in the query table Storing variables cur1 in

  DECLARE cur1 CURSOR FOR    SELECT user_account_id  FROM user_account_info '; 

# Basically everyone who uses a cursor has this sentence

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;   # Flag for loop termination , If there is no data in the cursor, set done by 1

 

# Open cursor

OPEN cur1;

            FETCH cur1 INTO useraccountid;                        

                WHILE (done=0) DO  

                           FETCH cur1 INTO useraccountid;                  

                           // Logical processing

              // End of cycle

               END WHILE ;                                                             

 // Close cursor    

 CLOSE cur1;  

The one above is a simple one Cursor template , When there is no data in the cursor done=1 Exit loop .

Nested cursors are needed today I found many examples on the Internet , Basically I can't understand Because I am also the first time to contact this . Since stored procedures can also be understood as a new programming language , So language It should be the same .

With the help of java The nested loop problem of language , So can cursors be implemented in this way . Let's try to see if we can

DECLARE useraccountid int DEFAULT 0;

     DECLARE daiid int DEFAULT 0;

         DECLARE done TINYINT(1) DEFAULT 0;# Is the data tag not found

         DECLARE cur1 CURSOR FOR select agent_account_id from agent_account_info where parent_id in ( select agent_account_id from agent_account_info where grandpa_id in (select agent_account_id from agent_account_info where agent_account=CONCAT(agentAcc)) or parent_id in (select agent_account_id from agent_account_info where agent_account=CONCAT(agentAcc)) or agent_account=CONCAT(agentAcc)) or agent_account=CONCAT(agentAcc);

         DECLARE cur2 CURSOR For select user_account_id from user_account_info WHERE user_account_type=2 and parent_id=CONCAT(daiid);

         DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;   # Flag for loop termination , If there is no data in the cursor, set done by 1

         # Open cursor

       OPEN cur1;

                        FETCH cur1 INTO daiid;# get data

                WHILE done=0 DO 

                                         open cur2;

                           fetch cur2 into  useraccountid;

                                                     WHILE done=0 DO

                                                               fetch cur2 into  useraccountid;

                                                    END WHILE ;                                                        

                                        CLOSE cur2; 

 

               SET done = 0; // Just remember this Others will be done according to the programming language you are familiar with

                                FETCH cur1 INTO daiid;# get data

                        END WHILE ;                                                             

      CLOSE cur1;  

If there's no above set done=0 Words By default, the inner loop flag is executed done=1 Will terminate the outer loop In other words, you can only perform one operation to push .

 

原网站

版权声明
本文为[Seven days at night]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221522489718.html