嵌套隐式游标不存在表或视图

时间:2017-01-04 17:44:25

标签: oracle

DECLARE
   sql_stmt varchar2(400);
   cursor c1 is SELECT view_name from all_views where owner = 'owner1' AND     view_name like 'IRV_%' OR view_name like 'RD_%' order by view_name;
BEGIN
for i IN c1 loop
  sql_stmt := 'create table new_table as select * FROM owner1.view1 minus select * FROM owner2.view1';
  dbms_output.put_line(sql_stmt);
  execute immediate sql_stmt;
  for ii IN (SELECT * from new_table) loop
    dbms_output.put_line('inner loop');
  end loop;  -- for ii
  execute immediate 'drop table new_table';
  exit when c1%NOTFOUND;
end loop;  -- for i
END;

我进入脚本输出:

ORA-06550:第9行,第32栏: PL / SQL:ORA-00942:表或视图不存在

第9行是:for II IN(SELECT * from new_table)loop

提前谢谢。

好的,这是我的新代码:感谢GurV的帮助。

DECLARE
CURSOR c1
 IS
   SELECT view_name
   FROM all_views
   WHERE owner = 'DBA_A'
   AND view_name LIKE 'IRV_%'
   OR view_name LIKE 'RD_%'
   ORDER BY view_name;
BEGIN
 FOR i IN c1
 LOOP
FOR ii IN ('select * FROM DBA_A.' || i.VIEW_NAME || ' minus select * FROM DBA_B.' || i.VIEW_NAME)
LOOP
    dbms_output.put_line('inner loop');
    -- put the results from the select in the FOR ii loop in a listAgg string
    -- do stuff
 END LOOP; -- for ii
END LOOP; -- for i
END;

END LOOP产生错误; - 对于ii

PLS-00103:遇到以下任何一种情况时遇到符号“END”:

dbms_output.put_line显示生成了正确的选择。 再次感谢。 富

1 个答案:

答案 0 :(得分:1)

您的代码不知道您将动态创建表格(怎么可能?)。

通常,The PL/SQL Compiler will check your block for the following before executing it:

  1. 检查语法。
  2. 检查语义和安全性,并解析名称。
  3. 生成(并优化)字节码(a.k.a. MCode)。
  4. 它位于代码失败的第二步,因为在编译之前没有一个名为new_table的表。

    我认为这里不需要动态SQL。此外,在光标上使用for循环时,不需要设置退出条件。你可以这样做:

    DECLARE
      CURSOR c1
      IS
        SELECT view_name
        FROM all_views
        WHERE owner = 'owner1'
        AND view_name LIKE 'IRV_%'
        OR view_name LIKE 'RD_%'
        ORDER BY view_name;
    BEGIN
      FOR i IN c1
      LOOP
        FOR ii IN (select * FROM owner1.view1 minus select * FROM owner2.view1)
        LOOP
          DBMS_OUTPUT.put_line('Hey there');
          -- do more stuff
        END LOOP; -- for ii
      END LOOP; -- for i
    END;