create trigger dbo.trg_active_problem
on dbo.active_problem
after update
not for replication
as
begin
if @@ROWCOUNT = 0 return
set nocount on;
if update (diagnosis)
begin
insert into dbo.dmlactionlog(
schemaname,
tablename,
affectedcolumn,
oldvalue,
newvalue
)
select
OBJECT_SCHEMA_NAME(@@PROCID, DB_ID()),
OBJECT_NAME(Object_id('active_problem'), DB_ID()),
'diagnosis',
d.diagnosis,
i.diagnosis
from inserted i
join deleted d on i.active_problem_id = d.active_problem_id
and coalesce(i.diagnosis, '') != coalesce(d.diagnosis, '')
end
if update (type)
begin
insert into dbo.dmlactionlog(
schemaname,
tablename,
affectedcolumn,
oldvalue,
newvalue
)
select
OBJECT_SCHEMA_NAME(@@PROCID, DB_ID()),
OBJECT_NAME(Object_id('active_problem'), DB_ID()),
'type',
d.[type],
i.[type]
from inserted i
join deleted d on i.active_problem_id = d.active_problem_id
and coalesce(i.type, '') != coalesce(d.type, '')
end
end;
go
我想创建这个触发器,这样我就不会在第一个if语句和第二个if语句中重复相同的代码。这里,类似的insert语句在两个if语句中重复,我如何才能更有效地管理它?
2条答案
按热度按时间31moq8wy1#
您可以尝试CROSS APPLY,根据条件为每个更新的行获取0到2个日志行
3pmvbmvn2#