如何在oracle中截断所有用户表?我有表约束的问题。
答案 0 :(得分:7)
declare
begin
for c1 in (select table_name, constraint_name from user_constraints) loop
begin
execute immediate ('alter table '||c1.table_name||' disable constraint '||c1.constraint_name);
end;
end loop;
for t1 in (select table_name from user_tables) loop
begin
execute immediate ('truncate table '||t1.table_name);
end;
end loop;
for c2 in (select table_name, constraint_name from user_constraints) loop
begin
execute immediate ('alter table '||c2.table_name||' enable constraint '||c2.constraint_name);
end;
end loop;
end;
/
答案 1 :(得分:6)
改进了上述脚本,以防您无法删除约束,因为存在依赖关系(以依赖于此约束的外键形式 - ORA-02297。)并通过打印全部(禁用,截断和启用) )陈述。
static IEnumerable<QueryResult> GetData(SqlCommand command)
{
using (var reader = command.ExecuteReader())
{
int index = 0;
do
{
while (reader.Read())
{
IDictionary<string, object> expando = new ExpandoObject();
for (int i = 0; i < reader.FieldCount; i++)
expando.Add(reader.GetName(i), reader.GetValue(i));
yield return new QueryResult(index, expando);
}
index++;
}
while (reader.NextResult());
}
}
答案 2 :(得分:2)
如果您有上述脚本失败的特殊约束,请进行改进:
set serveroutput on;
declare
begin
for c1 in (select y.table_name, y.constraint_name from user_constraints y, user_tables x where x.table_name = y.table_name) loop
begin
dbms_output.put_line('alter table '||c1.table_name||' disable constraint '||c1.constraint_name || ';');
execute immediate ('alter table '||c1.table_name||' disable constraint '||c1.constraint_name);
end;
end loop;
for t1 in (select table_name from user_tables) loop
begin
execute immediate ('truncate table '||t1.table_name);
end;
end loop;
for c2 in (select table_name, constraint_name from user_constraints) loop
begin
execute immediate ('alter table '||c2.table_name||' enable constraint '||c2.constraint_name);
end;
end loop;
end;
/
答案 3 :(得分:0)
无需变量
begin
for r in (select table_name from user_tables) loop
execute immediate 'truncate table ' || r.table_name;
end loop;
end;
此致 ķ
答案 4 :(得分:-1)
您可以输出,然后执行您喜欢的那个:
set serveroutput on;
begin
for r in (select table_name from user_tables) loop
dbms_output.put_line('truncate table ' || r.table_name);
end loop;
end;