sql drop - How can I run a script wich is generated from another script under Oracle DB? -


does know how tu run lines generated following query scripts on own right?

select 'drop table '||table_name||' cascade constraints;' user_tables; 

what i'm trying do, delete user tables , constraints on db (this oracle). output correct, want know how run lines without copy/pasting.

also, there more efficient way drop tables (including constraints)?

begin   in (select table_name user_tables)   loop     execute immediate ('drop table ' || i.table_name || ' cascade constraints');   end loop; end; / 

justin cave brought excellent point - following drop tables within user's schema starting @ outermost branches of hierarchy of dependencies, assuming foreign keys reference primary key, not unique constraint. tables without primary keys dropped last.

begin   in (select parent_table, max(tree_depth) tree_depth               (select parent.table_name parent_table,                            child.constraint_name foreign_key,                             child.table_name child_table,                            level tree_depth                       (select table_name, constraint_name                               user_constraints                              constraint_type = 'p'                            ) parent                             left join                            (select table_name, constraint_name,                                     r_constraint_name                               user_constraints                              constraint_type = 'r') child                               on parent.constraint_name =                                      child.r_constraint_name                            connect nocycle                               (prior child.table_name = parent.table_name)                    union                    select dt.table_name parent_table,                           null foreign_key, null child_table,                           0 tree_depth                      user_tables dt                     table_name not in                           (select table_name                              user_constraints                             constraint_type = 'p')                    )              group parent_table              order 2 desc            )   loop     execute immediate ('drop table ' || i.parent_table ||                         ' cascade constraints');   end loop; end; / 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -