USE [DSB]
GO
/****** Object: Trigger [dbo].[TRIG_DCSIBB] Script Date: 07/16/2019 15:54:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- 修改觸發器 [TRIG_DCSIBB]
ALTER trigger [dbo].[TRIG_DCSIBB] on [dbo].[DCSIBB] for insert, update as
-- update dbo.DCSIBB set IBB904=convert( varchar , getdate() , 121 ) from dbo.DCSIBB O join INSERTED I on (O.IBB001=I.IBB001) and (O.IBB002=I.IBB002)
-- 執行
begin
-- 聲明變量 @IBB006
declare @IBB006 int;
-- 為變量賦值 = insert時的 IBB006
set @IBB006=(select IBB006 from inserted)
-- 如果 變量小於1000 則執行 update
if @IBB006<1000
-- 注意:!!! from DCSIBB O join INSERTED I on (O.IBB001=I.IBB001) and (O.IBB002=I.IBB002) 此處IBB001,IBB002為組合主鍵,需要關聯進來進行update
update DCSIBB set IBB960 = '低於1000' from DCSIBB O join INSERTED I on (O.IBB001=I.IBB001) and (O.IBB002=I.IBB002) ;
else
if @IBB006=1000
update DCSIBB set IBB960 = '等於1000' from DCSIBB O join INSERTED I on (O.IBB001=I.IBB001) and (O.IBB002=I.IBB002);
if @IBB006>1000
update DCSIBB set IBB960 = '大於1000' from DCSIBB O join INSERTED I on (O.IBB001=I.IBB001) and (O.IBB002=I.IBB002);
end