Skip to main content

Mysql: Multiple loop cursor in procedure

DELIMITER $$

DROP PROCEDURE IF EXISTS `updatequestion` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `updatequestion`()

 BLOCK1: begin
     declare v_col1 int;
     declare no_more_rows1 boolean default FALSE;
     declare cursor1 cursor for
         select col1
         from   MyTable;
     declare continue handler for not found
         set no_more_rows1 := TRUE;


     open cursor1;
     LOOP1: loop
         fetch cursor1
         into  v_col1;
         if no_more_rows1 then
             close cursor1;
             leave LOOP1;
         end if;


         BLOCK2: begin
             declare v_col2 int;
             declare no_more_rows2 boolean default FALSE;
             declare cursor2 cursor for
                 select col2
                 from   MyOtherTable
                 where  ref_id = v_col1;
             declare continue handler for not found
                 set no_more_rows2 := TRUE;
             open cursor2;
             LOOP2: loop
                 fetch cursor2
                 into  v_col2;
                 if no_more_rows then
                     close cursor2;
                     leave LOOP2;
                 end if;
             end loop LOOP2;
         end BLOCK2;

     end loop LOOP1;

end BLOCK1$$


DELIMITER ;

Comments

Popular posts from this blog

Splitting Comma-Separated Values In MySQL

CREATE TABLE numbers (n INT);  INSERT INTO numbers VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9),     select id, substring_index( substring_index(email_recipients, ',', n), ',', -1 ) as email from dashboards join numbers on char_length(email_recipients) - char_length(replace(email_recipients, ',', '')) >= n - 1     Reference Site : https://www.sisense.com/blog/splitting-comma-separated-values-in-mysql/

Advance JS search

 <!DOCTYPE html> <html>     <head>         <title>             Choosen         </title>         <link href="chosen.min.css" rel="stylesheet" type="text/css">              <link crossorigin="anonymous" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.1/css/font-awesome.min.css" integrity="sha512-TECBPTD5Db97nlsJ/gGBBv9JLXg172YcGtrl/HHM4x9W++PPKEpHs61ogVd4x2omTgST1dIHoy2HeYWZzcZzHw==" referrerpolicy="no-referrer" rel="stylesheet"/>             <style type="text/css">                 .j-filters-level {     min-width: 400px;     border: 1...