Truncate all tables in Oracle database

今天開發團隊終於決定要把開發與測試用的 Oracle 資料庫清空重建了。在長達一年多的開發時間裡,這台測試 DB 是非常重要的開發工具。但長久以來承受了各式各樣亂七八糟的資料,也難以保證測試結果是否符合預期,所以大夥決定將它清空。

不過 Oracle 自己的 GUI 管理工具 SQL Developer 真是頗為難用。
Oracle SQL Developer

想一次清空 (truncate) 是沒有全選 -> 右鍵 -> 清空 這麼方便的事。但是近百個 table 又不想一個一個清… 就下指令吧。

begin
  for t in (select table_name from user_tables)
    loop
      execute immediate ' truncate table  '||t.table_name;
    end loop;
end;

這樣完成清除資料表的任務。


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *