在要清空表的数据的时候,用户可能会遇到主外键约束的问题,所以首先要disable所有的constraint。Oracle中有user_tables来记录所有有的表,user_constraints来记录所有的约束。
首先,要禁用所有的约束:
select 'alter table '|| t.table_name||' disable constraint '||t.constraint_name||';'
from user_constraints t where t.constraint_type = 'R'
and t.table_name in
(select ut.table_name from user_tables ut where ut.NUM_ROWS >= 100)
order by t.table_name;具体的条件可以自己限制一下
其次,清空表数据:
declare
BEGIN
for C_TAB in (SELECT 'Truncate table ' || T.TABLE_NAME D_SQL
FROM USER_TABLES T
WHERE T.TABLE_NAME not IN
('T_ACTU_INCIDENT_CODE')) loop
DBMS_OUTPUT.PUT_LINE (C_TAB.D_SQL||';');
--execute immediate ( C_TAB.D_SQL||';') ;
end loop;
end;
最后,肯定是要回复所有的约束(注意条件要一样):
select 'alter table '|| t.table_name ||' enable constraint '||t.constraint_name||';'
from user_constraints t where t.constraint_type = 'R'
and t.table_name in
(select ut.table_name from user_tables ut where ut.NUM_ROWS >= 100)
order by t.table_name;