Sql 插入到具有子选择的表中?

Sql 插入到具有子选择的表中?,sql,sql-server,sql-server-2014,Sql,Sql Server,Sql Server 2014,我有这个审计表: 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 delet

我有这个审计表:

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')
如何在审核表中插入记录,其中
项目id
必须是
inserted
deleted
中的
id
列,并且
history
列应包含该特定记录的xml

那么你是说这个

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

什么是xml?mytable中有xml列吗?@TabAlleman xml字符串应该放在
历史记录
列中。