DB2 的**宣告式全域暫存表(DGTT)**與 Oracle/Sybase 的暫存表機制差異極大。不瞭解其生命週期、Schema 隔離與提交行為,極易在生產環境踩坑。以下 6 大特性是開發與 DBA 必須掌握的核心知識。
create user temporary tablespace usrtemp pagesize 8k managed by automatic storage;SESSION schema,不受 CURRENT SCHEMA 影響SELECT * FROM temp_tab 等同 SELECT * FROM SESSION.temp_tabdeclare global temporary table session.temp_logged (id int) logged;
declare global temporary table session.my_temp (
id int,
name varchar(50)
)
with replace -- 若同名表存在則替換,避免報錯
on commit preserve rows -- 視業務決定保留/刪除
not logged; -- 或 logged

掌握這 6 大特性,DGTT 才能真正成為複雜邏輯暫存中間結果的利器,而非生產事故溫床。