1

我想从模式中删除对象,并且我已将所有不应删除的对象存储在表中,所有其他不在表中的对象都应删除。我有以下不起作用的代码可以在这里帮助我。我在网上搜索过只有一件事“删除所有对象”与我的情况无关。

DECLARE
    stringa VARCHAR2(100);
    stringb VARCHAR2(1000) := '';
    CURSOR cur IS
      SELECT *
      FROM   user_objects;
BEGIN
    FOR c IN cur LOOP
        BEGIN
            stringa := '';

            BEGIN
                SELECT tab_name
                INTO   stringb
                FROM   zretain_list;

                IF c.object_type = 'VIEW' THEN
                  stringa := 'drop view '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'TABLE'
                      AND c.object_name != stringb THEN
                  stringa := 'drop table  "'
                             || c.object_name
                             || '" cascade constraints';

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'SEQUENCE' THEN
                  stringa := 'drop sequence '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'PACKAGE' THEN
                  stringa := 'drop package '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'TRIGGER' THEN
                  stringa := 'drop trigger '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'PROCEDURE' THEN
                  stringa := 'drop procedure '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'FUNCTION' THEN
                  stringa := 'drop function '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'SYNONYM' THEN
                  stringa := 'drop synonym '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'INDEX' THEN
                  stringa := 'drop index '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'PACKAGE BODY' THEN
                  stringa := 'drop PACKAGE BODY '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                ELSIF c.object_type = 'DATABASE LINK' THEN
                  stringa := 'drop database link '
                             || c.object_name;

                  EXECUTE IMMEDIATE stringa;
                END IF;
            EXCEPTION
                WHEN OTHERS THEN
                  NULL;
            -- PURGE recyclebin
            END;
        END;
    END LOOP;
END;

/ 
4

1 回答 1

2

你可以从类似的东西开始

begin
  For i in (select object_type, object_name from user_objects o
            where object_name != 'ZRETAIN_LIST' and object_type in ('TABLE', 'FUNCTION', 'PROCEDURE', 'PACKAGE', 'SEQUENCE' )
            and not exists ( select 1 from zretain_list where tab_name = o.object_name ) )
  loop
    execute immediate 'drop '||i.object_type || ' ' || i.object_name ;
  end loop;
end;
/
于 2012-12-29T13:00:47.740 回答