Plsql 循环语句中表名的动态变量

Plsql 循环语句中表名的动态变量,plsql,oracle11g,Plsql,Oracle11g,我试图在另一个for循环语句中包含for循环语句,其中在内部for循环查询中使用的表_名称是first for循环的结果: declare l_insert_count pls_integer := 0; begin for row_outer in (select distinct table_name, item_type from TRANSFORMATION_MAPPING) LOOP l_insert_count := 0; for row in (select id f

我试图在另一个for循环语句中包含for循环语句,其中在内部for循环查询中使用的表_名称是first for循环的结果:

declare
l_insert_count pls_integer := 0;
begin

for row_outer in (select distinct table_name, item_type from TRANSFORMATION_MAPPING) LOOP
  l_insert_count := 0;


  for row in (select id from ***row_outer.table_name*** where NVL(platnost_do,sysdate)>=sysdate) LOOP

    execute immediate 'insert into ITEM_MAPPING(GUID,ID) VALUES(''CENDB:''' || sys_guid() || ',' || row.id || ')';

    l_insert_count  := l_insert_count + 1;  

    IF (l_insert_count > 999) THEN
        l_insert_count := 0;
        commit;
    END IF;

  END LOOP;

end LOOP;

commit;

end;
/
内环的主体要复杂得多。提供的代码只是为了显示我想做什么row\u outer.table\u name是我希望循环执行所需转换的表的变量名。在内部循环中,我需要根据外部循环中的表,将几百万行插入到几个不同的表中


非常感谢:)

我把它做成这样(未经测试):


通常我会避免在循环中提交,特别是对于插入(因为它们的开销最小,并且最难重新启动),但这取决于您。

谢谢您的回复,我会尝试一下。我将在每个外部循环中插入几百万行,所以我认为定期提交比有大的挂起事务要好。顺便说一句,如果打开游标的结果导致几百万行,那么使用该游标“安全”吗?一个大的挂起事务本身不是问题(1000行很小)。我会考虑在发生故障时的可重启性。也许您可以通过某种方式跟踪在外部循环中处理的表名,并对每个表提交一次?然后,也许你可以在你停止的地方重新启动外部循环。
declare
    l_insert_count pls_integer := 0;
    l_detail_cur sys_refcursor;
    l_detail_id integer;
begin
    for r in (
        select distinct table_name, item_type
        from   transformation_mapping
    )
    loop
        l_insert_count := 0;
        l_detail_id := null;

        open l_detail_cur for 'select id from '||r.table_name||' where nvl(platnost_do, sysdate) >= sysdate';

        loop
            fetch l_detail_cur into l_detail_id;
            exit when l_detail_cur%notfound;

            execute immediate 'insert into item_mapping(guid,id) values(''CENDB:''||sys_guid(), :id)' using l_detail_id;

            l_insert_count := l_insert_count + sql%rowcount;

            if l_insert_count > 999 then
                l_insert_count := 0;
                commit;
            end if;
        end loop;

        close l_detail_cur;

    end loop;

    commit;
end;