How to drop list of table from a schema in Oracle?

前端 未结 2 1053
粉色の甜心
粉色の甜心 2021-01-24 05:05

My Oracle scott schema contains table list like that

\'prefix_A\'
\'prefix_B\'
\'prefix_C\'
\'A\'
\'B\'
\'C\'

Now i want to drop list of tables

2条回答
  •  鱼传尺愫
    2021-01-24 05:13

    I used this to disable constraints, drop constraints and delete the tables.

     -- disable constraints on tables
    BEGIN
      FOR c IN
      (SELECT c.owner, c.table_name, c.constraint_name
       FROM user_constraints c, user_tables t
       WHERE c.table_name = t.table_name 
       AND t.table_name like 'WF_%'
       AND c.status = 'ENABLED'
       ORDER BY c.constraint_type DESC)
      LOOP
        dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" disable constraint ' || c.constraint_name);
      END LOOP;
    END;             
    
    -- drop the constraints
    begin
        for r in ( select table_name, constraint_name
                   from user_constraints where
                   table_name like 'WF_%' )
        loop
            execute immediate 'alter table '||r.table_name
                              ||' drop constraint '||r.constraint_name;
        end loop;
    end loop;
    
    -- drop the tables
    begin
         for trec in ( select table_name
                       from user_tables
                       where table_name like 'WF_%' )
         loop
             execute immediate 'drop table '||trec.table_name|| ' purge';
         end loop;
    end;
    

提交回复
热议问题