WITH const AS (SELECT rconst.table_name, rconst.constraint_name FROM user_constraints rconst, user_constraints pconst WHERE rconst.constraint_type = 'R' AND rconst.r_constraint_name = pconst.constraint_name AND pconst.constraint_type = 'P' ) SELECT 'ALTER TABLE '||const.table_name||' DISABLE CONSTRAINT '||const.constraint_name||';' rnme FROM const UNION ALL SELECT 'TRUNCATE TABLE '||ut.table_name||';' FROM user_tables ut UNION ALL SELECT 'ALTER TABLE ' ||const.table_name ||' ENABLE CONSTRAINT ' ||const.constraint_name ||';' rnme FROM const;to generate script to clean schema.
- All rows from all tables will be removed.
- You can NOT rollback this script even if you replace "truncate" with "delete" (alter statement permorms commit). But you can use flashback to query truncated data.
- You can write a pl\sql block with loop, using this select as a cursor and execute immediate command.
You also can avoid ORA-02266 when you need to truncate table (replace 'T-A-B-L-E--N-A-M-E' with name of the table you need to truncate)
WITH const AS (SELECT rconst.table_name, rconst.constraint_name FROM user_constraints rconst, user_constraints pconst WHERE rconst.constraint_type = 'R' AND rconst.r_constraint_name = pconst.constraint_name AND pconst.constraint_type = 'P' AND pconst.table_name = 'T-A-B-L-E--N-A-M-E' ) SELECT 'ALTER TABLE '||const.table_name||' DISABLE CONSTRAINT '||const.constraint_name||';' rnme FROM const UNION ALL SELECT 'TRUNCATE TABLE '||const.table_name||';' FROM const UNION ALL SELECT 'ALTER TABLE ' ||const.table_name ||' ENABLE CONSTRAINT ' ||const.constraint_name ||';' rnme FROM const;
No comments:
Post a Comment