oracle从架构中删除所选对象

时间:2012-12-29 12:05:06

标签: oracle

我想从架构中删除对象,并且我已经存储了不应该删除的所有对象,应该删除表中没有的所有其他对象。我有以下代码无法正常工作可以帮助我。我在网上搜索只有一件事“DELETE ALL OBJECT”与我的案子无关。

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;

/ 

1 个答案:

答案 0 :(得分: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;
/