【发布时间】:2020-12-05 01:57:18
【问题描述】:
下面的代码是我的Branch 表的审核触发器。它能够记录Branch 表中的任何编辑或插入更改。
但是,我有一个指向 BranchZone 表的外键 BranchZoneID。我想这样做,以便记录对此外键的任何更改,并且它将显示BranchZone 表中的BranchZoneName 而不是BranchZoneID。
我尝试使用代码为外键属性BranchZoneID 创建触发器。但是,我无法为其创建工作触发器。
BranchZoneID 的触发器不起作用。我需要改变什么?
create trigger Branch_Audit
on dbo.Branch
after insert, update
not for replication
as
begin
set nocount on;
declare @operation char(10) = case when exists (select * from deleted) then 'Edit' else 'Insert' end;
insert AuditLog
(TableName, ModifiedBy, AuditDateTime, AuditAction, ID, ChangedColumn, OldValue, NewValue)
select
'Branch', suser_sname(), getdate(), @operation, I.BranchZoneID,
'BranchName', convert(varchar(21),D.BranchName), convert(varchar(21),I.BranchName)
from inserted I
left outer join deleted D on I.BranchID = D.BranchID
where coalesce(I.BranchName,'') <> coalesce(D.BranchName,'')
and update(BranchName)
union all
select
'Branch', suser_sname(), getdate(), @operation, I.BranchID,
'BranchAddress', D.BranchAddress, I.BranchAddress
from inserted I
left outer join deleted D on I.BranchID = D.BranchID
where coalesce(I.BranchAddress,'') <> coalesce(D.BranchAddress,'')
and update(BranchAddress)
union all
select
'Branch', suser_sname(), getdate(), @operation, I.BranchID,
'BranchGeoLocationLat', D.BranchGeoLocationLat, I.BranchGeoLocationLat
from inserted I
left outer join deleted D on I.BranchID = D.BranchID
where coalesce(I.BranchGeoLocationLat,'') <> coalesce(D.BranchGeoLocationLat,'')
and update(BranchGeoLocationLat)
union all
select
'Branch', suser_sname(), getdate(), @operation, I.BranchID,
'BranchGeoLocationLong', D.BranchGeoLocationLong, I.BranchGeoLocationLong
from inserted I
left outer join deleted D on I.BranchID = D.BranchID
where coalesce(I.BranchGeoLocationLong,'') <> coalesce(D.BranchGeoLocationLong,'')
and update(BranchGeoLocationLong)
union all
select
'Branch', SUSER_SNAME(), GETDATE(), @operation, I.BranchID,
'BranchZoneID', OWD.BranchZoneName, NWD.BranchZoneName
from inserted I
left outer join deleted D on I.BranchID = D.BranchID
where coalesce(I.BranchZoneID,'') <> coalesce(D.BranchZoneID,'')
and update(BranchZoneID)
-- Fetch Branch Zone Name
on deleted.BranchZoneID = OWD.BranchZoneID
join dbo.BranchZone NWD
on inserted.BranchZoneID = NWD.BranchZoneID
end;
【问题讨论】:
-
那么有什么大不了的?您加入适当的表。您不能做的只是简单地“发明”一个别名并在您的 select 语句中引用它。并且为了将来参考,当您的问题包含错误时,您应该发布完整和实际的错误消息。而且您实际上应该提到您的代码会产生错误 - 这是因为 OWD 和 NWD 是尚未定义的别名。
标签: sql-server tsql triggers audit