Re: [SQL ] Trigger
※ 引述《ruby0104 (:))》之銘言:
: 最近剛開始用 Mysql...
: 想要建個 trigger 來自動更新 'Profit'
: tableA (itemID,SellPrice,Cost,Profit)
: create trigger tr_cal_profit after update on tableA for each row
: update tableA set profit=SellPrice-Cost
: where itemID=OLD.itemID;
換一個方式來看好了, 在trigger內不使用 SQL statement,而是直接在
更新前先做資料修改.
create trigger tr_cal_profit before update on tableA for each row
begin
if (new.itemID = old.itemID) then
set new.profit = new.SellPrice - old.Cost;
end if;
end;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 76.124.53.119
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):