Oracle过程游标循环错误

时间:2014-12-10 14:48:35

标签: sql oracle oracle11g cursor procedure

你能告诉我这个程序出了什么问题吗?

CREATE OR REPLACE PROCEDURE create_audit_tables (table_owner VARCHAR2)
IS
CURSOR c_tables (
  table_owner VARCHAR2)
IS
SELECT ot.owner AS owner, ot.table_name AS table_name
    FROM all_tables ot
   WHERE     ot.owner = table_owner
         AND ot.table_name NOT LIKE 'AUDIT_%'
         AND ot.table_name <> 'EXAUDIT'
         AND NOT EXISTS
                (SELECT 1
                   FROM EXAUDIT efa
                  WHERE ot.table_name = efa.tname)
         AND NOT EXISTS
                    (SELECT 1
                       FROM all_tables at
                      WHERE at.table_name = 'AUDIT_'||ot.table_name);
v_sql     VARCHAR2 (8000);
v_count   NUMBER := 0;
v_aud     VARCHAR2 (30);
BEGIN

FOR r_table IN c_tables (table_owner)
LOOP
  BEGIN
     v_aud := 'AUDIT_'||r_table.table_name;
     v_sql :=
           'create table '
        || v_aud
        || ' as select * from '
        || r_table.owner
        || '.'
        || r_table.table_name
        || ' where 1 = 1';

     DBMS_OUTPUT.put_line ('Info: ' || v_sql);

     EXECUTE IMMEDIATE v_sql;

     v_sql :=
           'alter table '
        || v_aud
        || ' add ( AUDIT_ACTION char(1), AUDIT_BY varchar2(50), AUDIT_AT TIMESTAMP)';

     EXECUTE IMMEDIATE v_sql;

     v_count := c_tables%ROWCOUNT;
  EXCEPTION
     WHEN OTHERS
     THEN
        DBMS_OUTPUT.put_line (
              'Failed to create table '
           || v_aud
           || ' due to '
           || SQLERRM);
  END;
END LOOP;

IF v_count = 0
THEN
  DBMS_OUTPUT.put_line ('No audit tables created');
ELSE
  DBMS_OUTPUT.put_line (v_count || ' audit tables created.');
END IF;
END;
/

我用show errors函数检查了它,它给了我这个:

LINE/COL ERROR
-------- --------------------------------------------
6/1      PL/SQL: SQL Statement ignored
16/3     PL/SQL: ORA-00906: missing left parenthesis

也许这个问题很简单,但是我看不到它,所以如果可以,请帮助我

1 个答案:

答案 0 :(得分:0)

创建表

后,我可以毫无问题地编译程序
create table EXAUDIT (tname varchar2(100));

检查程序状态:

select status from user_objects where lower(object_name) = 'create_audit_tables';