Oracle:尝试使用动态表名列表循环插入语句

时间:2014-11-15 18:02:24

标签: oracle

我不太了解这里找到的解决方案: Selecting Values from Oracle Table Variable / Array?

我有一个表名列表。我想将它们作为一个数组循环,使用它们的值作为表来搜索。

TMP_DORMANT_FILTERS表名的物理表。下面的数组是相同的列表。 LM_DORMANT_EMAIL是电子邮件地址列表。

我想检查表格列表中是否存在休眠电子邮件地址。我意识到我可以写12次相同的查询来搜索每个表。但这不会提高我的SQL技能。

这是我的阵列尝试。在这种尝试中,Oracle不喜欢我在不存在的地方调用数组值的方式:sql:

DECLARE 
TYPE array_t IS VARRAY(12) OF VARCHAR2(25);

ARRAY array_t := array_t('BT_ABANDONED_HIST', 'BT_BROWSED_HIST', 'BT_PURCHASED_HIST',       'CM_ABANDONED_HIST', 'CM_BROWSED_HIST', 'CM_PURCHASED_HIST', 'CM_PAGE_VIEWS_HIST', 'MB_ABANDONED_HIST', 'MB_BROWSED_HIST', 'MB_CARTED_HIST', 'MB_PAGE_VIEWS_HIST', 'MB_PURCHASED_HIST');

BEGIN
FOR i IN 1..array.count LOOP
INSERT INTO TMP1_DORMANT_EMAIL
SELECT feed.EMAIL_ADDRESS
FROM LM_DORMANT_EMAIL feed
WHERE NOT EXISTS (
        SELECT 1 FROM array(i) hist
            WHERE ACTIVITY_DATE >= TRUNC(SYSDATE - 90)
            AND hist.EMAIL = feed.EMAIL_ADDRESS
        );

COMMIT;

END LOOP;

END;
/

或者使用上面链接中找到的解决方案,我试过了。 Oracle没有认识到我在开始部分下插入dormant_filters。它告诉我我的物理表TMP_DORMANT_FILTERS不存在:

CREATE GLOBAL TEMPORARY TABLE dormant_filters
( filters varchar2(100)
)
ON COMMIT DELETE ROWS;

BEGIN 
INSERT INTO dormant_filters
  ( filters )
  ( SELECT TABLE_NAMES 
      FROM TMP_DORMANT_FILTERS 
  ); 

 FOR j IN ( SELECT filters FROM dormant_filters ) LOOP 
    INSERT INTO TMP1_DORMANT_EMAIL
        SELECT feed.EMAIL_ADDRESS, j as DORMANT_SOURCE
        FROM LM_DORMANT_EMAIL feed
        WHERE NOT EXISTS (
            SELECT 1 FROM j hist
            WHERE feed.ACTIVITY_DATE >= TRUNC(SYSDATE - 90)
            AND hist.EMAIL = feed.EMAIL_ADDRESS
            );
        NULL;
END LOOP; 
COMMIT;
END; 
/

1 个答案:

答案 0 :(得分:0)

此问题需要动态SQL。绑定变量可用于值,但不能用于对象。

declare 
    type array_t is varray(12) of varchar2(25);
    array array_t := array_t('BT_ABANDONED_HIST', 'BT_BROWSED_HIST', 'BT_PURCHASED_HIST', 'CM_ABANDONED_HIST', 'CM_BROWSED_HIST', 'CM_PURCHASED_HIST', 'CM_PAGE_VIEWS_HIST', 'MB_ABANDONED_HIST', 'MB_BROWSED_HIST', 'MB_CARTED_HIST', 'MB_PAGE_VIEWS_HIST', 'MB_PURCHASED_HIST');
begin
    for i in 1 .. array.count loop
        execute immediate '
            INSERT INTO TMP1_DORMANT_EMAIL
            SELECT feed.EMAIL_ADDRESS
            FROM LM_DORMANT_EMAIL feed
            WHERE NOT EXISTS (
                    SELECT 1 FROM '||array(i)||' hist
                        WHERE ACTIVITY_DATE >= TRUNC(SYSDATE - 90)
                        AND hist.EMAIL = feed.EMAIL_ADDRESS
                    )
        ';
        commit;
    end loop;
end;
/

<强>更新

如果每个表的列名不同,您可以使用数据字典选择正确的列名。

declare 
    type array_t is varray(12) of varchar2(25);
    array array_t := array_t('BT_ABANDONED_HIST', 'BT_BROWSED_HIST', 'BT_PURCHASED_HIST', 'CM_ABANDONED_HIST', 'CM_BROWSED_HIST', 'CM_PURCHASED_HIST', 'CM_PAGE_VIEWS_HIST', 'MB_ABANDONED_HIST', 'MB_BROWSED_HIST', 'MB_CARTED_HIST', 'MB_PAGE_VIEWS_HIST', 'MB_PURCHASED_HIST');
    v_column_name varchar2(30);
begin
    for i in 1 .. array.count loop
        select column_name
        into v_column_name
        from all_tab_columns
        where owner = 'SCHEMA NAME'
            and table_name = array(i)
            and column_name in ('ACTIVITY_TIME','DATE_ABANDONED');

        execute immediate '
            INSERT INTO TMP1_DORMANT_EMAIL
            SELECT feed.EMAIL_ADDRESS
            FROM LM_DORMANT_EMAIL feed
            WHERE NOT EXISTS (
                    SELECT 1 FROM '||array(i)||' hist
                        WHERE '||v_column_name||' >= TRUNC(SYSDATE - 90)
                        AND hist.EMAIL = feed.EMAIL_ADDRESS
                    )
        ';
        commit;
    end loop;
end;
/