0

我正在尝试在 Oracle 数据库中使用 Bulk all 和 Forall:

来自Procedure的原始代码如下:

IF NVL(v_mc,0) != 0 THEN
        FOR rec IN
        (SELECT a.testid,
            SUM(pct * NVL(cap,0))/v_mc lead1
            BULK COLLECT INTO testids1, testids2
        FROM testtable a
        WHERE a.id      = n_id
        AND a.type      =n_type

        GROUP BY a.testid;
        )
        LOOP
            UPDATE testtable
            SET LEAD1    =ROUND(testids2(i),2)
            WHERE tid  = n_id
            AND type  = n_type
            AND testid   =testids1(i);
        END LOOP;
END IF;

所以在 select 语句中,我使用 Sum 函数并在这里使用别名。

代码,我写的使用 Bulk collect 和 Forall 如下:

PROCEDURE test
IS

    TYPE test1Tab IS TABLE OF sh_rpt_temp_peer_wip.test1%TYPE;
    TYPE test2Tab IS TABLE OF testtable.lead1%TYPE;
    testids1 testidTab; --Error 1 and Error 2
    testids2 LeadTab;

BEGIN

IF NVL(v_mc,0) != 0 THEN

     SELECT testid,
            SUM(pct * NVL(cap,0))/v_mc lead1
            BULK COLLECT INTO testids1, testids2
        FROM testtable a               --Error 3
        WHERE a.id      = n_id
        AND a.type      =n_type

        GROUP BY a.testid ORDER BY a.testid;

        FORALL i IN testids1.FIRST..testids1.LAST
          UPDATE testtable
            SET LEAD1    =ROUND(testids2(i),2)
            WHERE tid  = n_id  --Error 3
            AND type  = n_type
            AND testid   =testids1(i);

END IF;

END;

但是在我编译过程时,我遇到了多个错误。我对 PL/SQL 很陌生。请让我知道我是否可以在批量收集中检索计算值作为列?我在程序中遇到以下错误:

  • 错误 1) PL/SQL: 项目被忽略
  • 错误 2) 必须声明组件“LEAD”
  • 错误 3) 表达式类型错误

请让我知道这里有什么问题

谢谢

4

1 回答 1

0

正如我所确定的,您所引用的集合类型不在过程的范围内,可能是您已在全局范围内声明。我修改了你的代码,试一试,希望它对你有用。

PROCEDURE test
IS

    TYPE test1Tab IS TABLE OF testtable.testid%TYPE;
    TYPE test2Tab IS TABLE OF number;
    testids1 test1Tab; //Error 1 and Error 2
    testids2 test2Tab;

BEGIN

IF NVL(v_mc,0) != 0 THEN

     SELECT testid,
            SUM(pct * NVL(cap,0))/v_mc lead
            BULK COLLECT INTO testids1, testids2
        FROM testtable a               //Error 3
        WHERE a.id      = n_id
        AND a.type      =n_type
        GROUP BY a.testid ORDER BY a.testid;

        FORALL i IN testids1.FIRST..testids1.LAST
          UPDATE testtable
            SET LEAD    = ROUND(testids2(i),2)
            WHERE tid  = n_id   //Error 3
            AND type  = n_type
            AND testid   = testids1(i);

END IF;
END;
于 2018-06-13T05:44:54.820 回答