工作上時常會用到一些測試資料表,通常會叫做 ooxx_test,一旦時間長了就會累積很多垃圾.
在postgresql上可以使用pg_tables查詢目前資料庫內的資料表 , 搭配 copy 這個命令就能快速產生批次刪除垃圾資料表
1 2 3 4 5
| copy (select 'drop table "' || tablename || '"' from pg_tables where tablename like '%test%') to 'D:\DropTestTable.txt'
|
更狠心想要直接執行的話則可以使用以下方法 , 防止沒睡飽特別加上註解 , 要用時在把註解打開!
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| do $$ declare del_name text; begin
select array_to_string( array(select quote_ident( tablename ) into del_name from pg_tables where tablename like '%test%'), ','); RAISE NOTICE 'drop table %' , del_name; --execute 'drop table ' || del_name ; end; $$;
|