Oracle 脚本:清空当前用户下所有表的数据


这段时间,有个配置库需要导给不同人,每个人导到库后都需要清空里面的各个表,如果逐个表来点的话,挺麻烦的,故写了以下 PL/SQL 脚本。

  1. /**   
  2.    将所有的 table 清空(可回滚)  
  3. **/  
  4. declare  
  5.   -- 指向所有 table 的游标   
  6.   cursor c_t is   
  7.     select table_name  
  8.     from user_tables;  
  9.     
  10.   table_name user_tables.table_name%type;  
  11. begin  
  12.   open c_t;  
  13.   loop   
  14.        fetch c_t into table_name;   
  15.        exit when c_t%notfound;  
  16.          
  17.        -- 用 delete 而不用 truncate 是为了能户用户回滚,减少误操作   
  18.        execute immediate 'delete from ' || table_name;  
  19.   end loop;  
  20.   close c_t;  
  21. end;  

相关内容