在没有循环的情况下将值赋给嵌套表

时间:2017-05-31 00:02:00

标签: oracle plsql

说我有表XX_TEST_DATA并且它用于填充下面PL / SQL块中的嵌套表(请注意注释):

DECLARE

    CURSOR XX_DATA_CUR
    IS
    SELECT  *
    FROM    XX_TEST_DATA;

    TYPE TYP_XX_TEST IS TABLE OF XX_TEST_DATA%ROWTYPE INDEX BY PLS_INTEGER;
    XX_REC  TYP_XX_TEST;

BEGIN
    OPEN    XX_DATA_CUR;
    FETCH   XX_DATA_CUR
    BULK COLLECT 
    INTO    XX_REC;
    CLOSE   XX_DATA_CUR;

    for i in 1..XX_REC.count loop

        XX_REC(i).BATCH_NAME := 'Batch 1'; -- This is the Line

    end loop;

END;

我想将值“Batch 1”分配给嵌套表中的所有记录。如果没有循环遍历所有记录,这是否可行? 如下所示:

BEGIN
    OPEN    XX_DATA_CUR;
    FETCH   XX_DATA_CUR
    BULK COLLECT 
    INTO    XX_REC;
    CLOSE   XX_DATA_CUR;

    XX_REC.BATCH_NAME := 'Batch 1'; -- 

END;

我知道上面的块不会起作用,但我希望能够实现这样的目标。

测试表的DDL

Create table XX_TEST_DATA
(
    XX_ID          NUMBER
,   XX_DATA1       VARCHAR2(100)
,   XX_DATA2       VARCHAR2(100)
,   BATCH_NAME  VARCHAR2(100)
);    

1 个答案:

答案 0 :(得分:1)

该值可以设置为SELECT的一部分,然后就不需要更新集合。

DECLARE
    TYPE TYP_XX_TEST IS TABLE OF XX_TEST_DATA%ROWTYPE INDEX BY PLS_INTEGER;
    XX_REC  TYP_XX_TEST;
BEGIN
    SELECT XX_ID, XX_DATA1, XX_DATA2, 'Batch 1'
    BULK COLLECT INTO XX_REC
    FROM XX_TEST_DATA;
END;
/