1

被困在这个问题上大约 2 个小时,我有这个过程,它需要一个边和一个集合并返回一个布尔值leaf来测试某个节点是否是叶子。但是,该测试仅适用于左侧节点,一旦进入第二个循环,它就会崩溃,我无法弄清楚原因:

CREATE  PROCEDURE `is_leaf`(IN `edge` VARCHAR(12), IN `eset` VARCHAR(512), OUT `leaf` INT)
BEGIN

declare lnode char(6) default substring_index(edge, ':', 1); -- left node of edge.
  declare rnode char(6) default substring_index(edge, ':', -1); -- right node of edge.
  declare cedge char(12);
  declare lnode_in_cedge char(6);
  declare rnode_in_cedge char(6);
  declare found int default 1;
   declare OGset varchar(512) default '';
  SET OGset= concat('',eset);

--CHECKS LEFT NODE OF EDGE AGAINST ALL EDGES IN SET
WHILE length(eset)!=0 AND found!=2 DO
set cedge = substring_index(eset, ',', 1);
SET lnode_in_cedge= substring_index(cedge, ':', 1);
SET rnode_in_cedge= substring_index(cedge, ':', -1);

--IF LEFT NODE IS FOUND TO HAVE COMMON VERTEX, THE SAME TEST IS RUN FOR THE RIGHT ONE
IF lnode= lnode_in_cedge OR lnode= rnode_in_cedge THEN
    BEGIN
        SET leaf=false;

    SET eset= OGset;
    WHILE length(eset)!=0 AND found !=2 DO
    set cedge = substring_index(eset, ',', 1);
    SET lnode_in_cedge= substring_index(cedge, ':', 1);
    SET rnode_in_cedge= substring_index(cedge, ':', -1);
        IF rnode= lnode_in_cedge OR rnode= rnode_in_cedge THEN
            BEGIN
            SET leaf= false;
            SET found= 2;
            END;
         END IF;
         SET eset  = REMOVE_FIRST(eset);
     END WHILE;
     END;
END IF;

SET eset  = REMOVE_FIRST(eset);
END WHILE;



IF found=1 THEN
SET leaf=true;
END IF;


END
4

1 回答 1

0

我通过打破循环让它工作,如果有人找到以前的代码不起作用的原因,请告诉我。我不太了解 MySQL 中的嵌套循环。这是工作代码:

CREATE PROCEDURE `is_leaf`(IN `edge` VARCHAR(12), IN `eset` VARCHAR(512), OUT `leaf` INT)
BEGIN

declare lnode char(6) default substring_index(edge, ':', 1); -- left node of edge.
  declare rnode char(6) default substring_index(edge, ':', -1); -- right node of edge.
  declare cedge char(12);
  declare lnode_in_cedge char(6);
  declare rnode_in_cedge char(6);
  declare found int default 1;
  declare OGset varchar(512) default '';
  SET OGset= concat('',eset);

WHILE length(eset)!=0  DO
set cedge = substring_index(eset, ',', 1);
SET lnode_in_cedge= substring_index(cedge, ':', 1);
SET rnode_in_cedge= substring_index(cedge, ':', -1);
IF lnode= lnode_in_cedge OR lnode= rnode_in_cedge THEN
    set found=0;
END IF;

SET eset  = REMOVE_FIRST(eset);
END WHILE;

IF found=0 THEN
BEGIN
    SET eset= OGset;
    WHILE length(eset)!=0  DO
    set cedge = substring_index(eset, ',', 1);
    SET lnode_in_cedge= substring_index(cedge, ':', 1);
    SET rnode_in_cedge= substring_index(cedge, ':', -1);
        IF rnode= lnode_in_cedge OR rnode= rnode_in_cedge THEN
            BEGIN
            SET leaf= false;
            SET found= 2;
            END;
         END IF;
         SET eset  = REMOVE_FIRST(eset);
     END WHILE;
END;
END IF;


IF found!=2 THEN
SET leaf=true;
END IF;


END
于 2012-12-06T09:25:56.153 回答