mysql递归存储过程无法编码sql语句

dxpyg8gm  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(274)

我创建了一个递归存储过程disptree来显示bom(bom表)树。

BEGIN
    DECLARE DONE BIT DEFAULT FALSE;
    DECLARE BOMID INT DEFAULT 0;
    DECLARE CQTY FLOAT DEFAULT 0;
    DECLARE NBOMID INT DEFAULT 0;
    DECLARE REQQTY FLOAT DEFAULT 0;
    DECLARE CAL_REQDATE DATE;
    DECLARE CLEADTIME INT DEFAULT 0;
    DECLARE CALWEEK INT DEFAULT 0;
    DECLARE INSQL TEXT;
    DECLARE MRP_ID INT DEFAULT 0;

    DECLARE CR_SQL1 CURSOR FOR SELECT BOM.BOMID FROM BOM WHERE BOM.PRODUCTID=PRODUCTID;

    DECLARE CR_SQL2 CURSOR FOR SELECT BOMPRT.PRODUCTID,BOMPRT.QTY FROM BOMPRT WHERE BOMPRT.BOMID=BOMID ORDER BY BOMPRT.PRODUCTID;

    DECLARE CR_SQL4 CURSOR FOR SELECT PRODUCT.LEADTIME FROM PRODUCT WHERE PRODUCT.PRODUCTID=CPRODUCTID;

    DECLARE CR_SQL5 CURSOR FOR SELECT MRP.MRPID FROM MRP WHERE MRP.PRODUCTID=CPRODUCTID AND MRP.SCH_WEEK=CALWEEK;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE:=TRUE;
    SET max_sp_recursion_depth=255; 

    OPEN CR_SQL1;
        FETCH CR_SQL1 INTO BOMID;
    CLOSE CR_SQL1;

    IF BOMID IS NULL THEN
        SET BOMID=0;
    END IF;

    OPEN CR_SQL2;
    LP_LOP1:LOOP
        FETCH CR_SQL2 INTO CPRODUCTID,CQTY;

        IF DONE THEN
            LEAVE LP_LOP1;
        END IF;

         SET REQQTY=GENERATED_QTY*CQTY;

          OPEN CR_SQL4;
             FETCH CR_SQL4 INTO CLEADTIME;
          CLOSE CR_SQL4;

        -- SELECT CLEADTIME;

        IF CLEADTIME IS NULL THEN
             SET CLEADTIME=0;
        END IF;

          IF CLEADTIME>0 THEN
              SELECT DATE_ADD(REQDATE,INTERVAL CLEADTIME DAY) INTO CAL_REQDATE;
          ELSE
              SET CAL_REQDATE=REQDATE;
          END IF;

          SELECT WEEK(CAL_REQDATE) INTO CALWEEK;

          IF CALWEEK IS NULL THEN
            SET CALWEEK=0;
          END IF;

        select CAL_REQDATE,CPRODUCTID,CALWEEK;

          OPEN CR_SQL5;
             FETCH CR_SQL5 INTO MRP_ID;
          CLOSE CR_SQL5;

            CALL disptree(CPRODUCTID,REQQTY,REQDATE,PRODUCTID);

    END LOOP;
    CLOSE CR_SQL2;

END

在上面的存储过程中,如果我删除下面三行

OPEN CR_SQL5;
    FETCH CR_SQL5 INTO MRP_ID;
CLOSE CR_SQL5;

存储在层次结构中执行并显示产品。
我有下面四个表(列,列)
物料清单(bomid,productid)
bomprt(bomid、bomprtid、productid)
产品(产品ID,交货期)
mrp(mrpid、productid、schweek)
所有列都是整数类型
有人能告诉我我犯了什么错误吗。

szqfcxe2

szqfcxe21#

我有办法了。
在存储过程调用self-done标志设置为true之后。它禁止进一步获取记录。
我添加了以下代码

CALL disptree(CPRODUCTID,REQQTY,REQDATE,PRODUCTID);
 set done=false;

如果有任何其他解决方案,请发表意见。

相关问题