我正在生成的示例表和生成的xml具有使用CTE插入一组行和xml生成的列的从它如何通过使用CTE到一个表中单个查询
declare @t table (ID int, PersonID int, Type varchar(10), Name varchar(10),
Category varchar(10), value int)
insert @t values
(1, 1234, 'xtype', 'John', 'abc', 200),
(2, 1234, 'ytype', 'John', 'xyz', 230),
(3, 1234, 'ztype', 'John', 'ccc', 220),
(4, 2222, 'xtype', 'Jim', 'abc', 200),
(5, 3333, 'xtype', 'Tim', 'abc', 500)
; with cte
as (
select distinct PersonID from @t
)
select
(select
PersonID,
(select
Type,
Name,
category,
value
from
@t t
where
t.PersonID = cteInner.PersonID
for xml path('SpecifiedType'), type)
from
cte cteInner
where
cteInner.PersonID = cteOuter.PersonID
for xml path(''), type, root('data')) as xmldetail
from
cte cteOuter
现在我要插入的下面的数据到另一个表包括XML,我创建数据
Example Table
PersonID Name xmldetail
1234 John (GeneratedXmlFrom using cte)
2222 Jim (GeneratedXmlFrom using cte)
3333 Tim (GeneratedXmlFrom using cte)
感谢,它的工作 – user3494425
INSERT INTO表名 ([是PersonID] ,[名] ,[xmldetail]) 选择* from cte – user3494425
我无法插入表中,我写了下面的插入语句,但没有插入,请你帮我 – user3494425