Oracle 11g:如何在BULK COLLECT中动态选择多个列

时间:2017-03-30 20:08:34

标签: oracle plsql oracle11g bulk-collect

我有一个类似下面示例的查询,我希望从包含4列的表中使用连接动态批量收集2列,但我不断收到错误,该变量不在选择列表中#39 ;。我相信这是因为这是因为查询的列数比表格少,但我不知道如何绕过它。

DECLARE
    TYPE test_table_type IS TABLE OF TEST_PRODUCTS%ROWTYPE;
    BULK_COLLECTOR test_table_type;
BEGIN
    EXECUTE IMMEDIATE
    'SELECT tp.PRODUCT AS PRODUCT, td.ALT_DESCRIPTION AS DESCRIPTION
    FROM TEST_PRODUCTS tp
    JOIN ALT_DESCRIPTIONS td ON tp.ALT_DESCRIPTION_ID = td.ALT_DESCRIPTION_ID'
    BULK COLLECT INTO BULK_COLLECTOR;
END;

以下是表格的示例:

CREATE TABLE TEST_PRODUCTS
(
    PRODUCT_ID NUMBER,
    PRODUCT VARCHAR2(50),
    DESCRIPTION VARCHAR2(255)
    ALT_DESCRIPTION_ID NUMBER,
    INVENTORY_CODE VARCHAR2(100)
);

CREATE TABLE ALT_DESCRIPTIONS
(
    ALT_DESCRIPTION_ID NUMBER,
    ALT_DESCRIPTION VARCHAR2(255)
);

1 个答案:

答案 0 :(得分:3)

您正在从两个不同的表中进行选择,并尝试将其放入一个表的行类型中。

你可能想采取不同的方法 - 像这样:

DECLARE
TYPE test_record IS RECORD
(
  PRODUCT VARCHAR2(50),
  ALT_DESCRIPTION VARCHAR2(255)
);
type test_tab is table of test_record;
BULK_COLLECTOR test_tab;
BEGIN
   EXECUTE IMMEDIATE
   'SELECT tp.PRODUCT AS PRODUCT, td.ALT_DESCRIPTION AS DESCRIPTION
   FROM TEST_PRODUCTS tp
   JOIN ALT_DESCRIPTIONS td ON tp.ALT_DESCRIPTION_ID = td.ALT_DESCRIPTION_ID'
   BULK COLLECT INTO BULK_COLLECTOR;
END;