Nested Cursors in Mysql

后端 未结 3 1304
灰色年华
灰色年华 2020-12-24 03:08

I have three tables.
Project(Id), attribute(Id), project_attribute(Id, project_id, attribute_id).

I want to create records in project_at

3条回答
  •  -上瘾入骨i
    2020-12-24 03:39

    After the first iteration within the inner cursor "attribute_done" variable is set to "true". And it remains "true" for every next iterations.

    This causes every next iterations to skip the inner loop.

    A sample nested cursor is illustrated below.


    CREATE TABLE `parent` (
      `a` int(11) DEFAULT NULL
    ) ENGINE=InnoDB
    CREATE TABLE `child` (
      `a` int(11) DEFAULT NULL,
      `b` varchar(20) DEFAULT NULL
    ) ENGINE=InnoDB
    
    insert into parent values (1),(2),(3);
    
    insert into child values (1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b');
    
    ----------------------------------
    drop procedure if exists nestedCursor;
    create procedure nestedCursor()
    BEGIN   
        DECLARE done1, done2 BOOLEAN DEFAULT FALSE;  
        DECLARE parentId,childId int;
        DECLARE childValue varchar(30);
    
        DECLARE cur1 CURSOR FOR SELECT a FROM parent;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = TRUE;
    
        open cur1;
        loop1: LOOP
        FETCH FROM cur1 INTO parentId;
        IF done1 THEN
            CLOSE cur1;
            LEAVE loop1;
        END IF;
    
        BLOCK1 : BEGIN
        DECLARE cur2 CURSOR FOR SELECT a,b FROM child where a = parentId;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;
    
    
        open cur2;
        loop2 : LOOP
        FETCH FROM cur2 INTO childId,childValue;  
            if done2 THEN
            CLOSE cur2;
            SET done2 = FALSE;
            LEAVE loop2;
            end if;
            select parentId,childId,childValue;
    
        END LOOP loop2;
        END BLOCK1;
        END loop loop1;
    END;
    

提交回复
热议问题