我有这个审核表:
create table audit(
id int IDENTITY(1, 1) NOT NULL,
item_id int NOT NULL,
history text NOT NULL,
)
我有这个触发器:
create trigger tr_mytable_audit ON mytable after insert, update, delete
as
select *
from inserted as i
full join deleted as d
on i.id = d.id
for xml path('row'), root('rows')
如何在审核表中插入记录,其中item_id
必须是id
或inserted
的{{1}}列,且deleted
列应包含该特定记录的xml。
答案 0 :(得分:0)
你的意思是这个?:
create trigger tr_mytable_audit ON mytable after insert, update, delete
as
INSERT INTO audit(item_id, history)
SELECT i1.id, (
select *
from inserted as i
full join deleted as d
on i.id = d.id
where i.id=i1.id
for xml path('row'), root('rows')
)
from inserted i1
UNION
SELECT i1.id, (
select *
from inserted as i
full join deleted as d
on i.id = d.id
where d.id=d1.id
for xml path('row'), root('rows')
)
from deleted d1