2009年1月22日 星期四

[Oracle] Trigger - 「:NEW」的使用

今天試寫一支 trigger,目的是要讓每一列的 action 欄位更新為 1 時,自動塞入日期與時間的資料,第一次用到「:new」,因此記錄一下。

以下是程式碼:

create or replace trigger tri_fill_datetime_to_message
--只有在更新資料後才需要加入日期時間資訊
before update
on mytable
for each row
begin
--當 action 更新的值為 1 時,在執行以下動作
if :new.action = 1 then
--設定新資料的日期時間資訊
:new.log_date := to_char(sysdate, 'YYYYMMDD');
:new.log_time := to_char(sysdate, 'HH24MISS');
:new.log_datetime := sysdate;
end if;
end tri_fill_datetime_to_message;


參考資料

沒有留言:

張貼留言