在sqlserver中用INSERTED和deleted,不区分大小写的。下面是两个例子。
CREATE TRIGGER dbo.AccountingRepTrigger1 ON dbo.AccountingRep
FOR UPDATE AS
IF Update(amtUpdated)
begin
declare @a1 float
declare @a2 float
select @a1=(select amtUpdated from Inserted)
select @a2=(select amtUpdated from deleted)
update accountingRep
set balance=balance + @a1 - @a2
where tableCode=(select tableCode from deleted)
and tableNo=(select tableNo from deleted)
end
CREATE TRIGGER [trgAtoComplete] ON dbo.TpcTPreMatMaster
FOR UPDATE
AS
IF Update(status)
BEGIN
declare @instNo char(10)
declare @items tinyint
declare @recType char(1)
declare @status char(1)
declare @colorNo smallint
select @instNo=instNo,@items=items,
@recType=recType,@status=status,
@colorNo=colorNo from INSERTED
IF @status='D'
BEGIN --删除细档
update TpcTPreMatMDetail
set status = 'D'
where instNo = @instNo
and items=@items
and colorNo=@colorNo
AND recType=@recType
return
END
IF @status<>'Y'
return
--如果存在其它颜色还有'N'状态的,则退出
IF EXISTS
(SELECT * FROM TpcTPreMatMaster
WHERE instNo=@instNo
AND items=@items
AND recType=@recType
AND status='N')
return
--如果存在其它颜色还有'N'状态的,则退出
IF (@recType='V')
BEGIN
update TpcTPreMatDetail
set vampLack = 'Y'
where instNo = @instNo
and items=@items
AND status<>'D'
END
ELSE IF (@recType='B')
BEGIN
update TpcTPreMatDetail
set bigSoleLack = 'Y'
where instNo = @instNo
and items=@items
AND status<>'D'
END
ELSE IF (@recType='M')
BEGIN
update TpcTPreMatDetail
set midSoleLack = 'Y'
where instNo = @instNo
and items=@items
AND status<>'D'
END
ELSE IF (@recType='L')
BEGIN
update TpcTPreMatDetail
set lastLack = 'Y'
where instNo = @instNo
and items=@items
AND status<>'D'
END
END