如何在T-SQL中合并XML?

dud*_*er4 2 xml t-sql sql-server

似乎没有任何数量的阅读文档会帮助我.考虑简化的例子:

declare @table1 table ( id int, parent xml )
insert @table1 values( 1, '<Root></Root>' )
declare @table2 table ( id int, guts xml )
insert @table2 values( 1, '<Guts>hi mom!</Guts>' )

select t1.parent.query('')
from @table1 t1 inner join @table2 t2 on t1.id = t2.id
Run Code Online (Sandbox Code Playgroud)

将传递给查询函数以生成此结果的内容是什么?

<Root><Guts>hi mom!</Guts></Root>
Run Code Online (Sandbox Code Playgroud)

Mic*_*art 5

以下是未设置的基础,但可能会有所帮助(仅限SQL2008)

declare @table1 table ( id int, parent xml )
insert @table1 values( 1, '<Root></Root>' )
declare @table2 table ( id int, guts xml )
insert @table2 values( 1, '<Guts>hi mom!</Guts>' )

DECLARE @id int;
DECLARE @results table (id int, results xml);

DECLARE idCursor CURSOR FOR 
    select id from @table1
OPEN idCursor

FETCH NEXT FROM idCursor INTO @id

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @parent xml, @guts xml

    SELECT @parent = parent FROM @table1 where id = 1;
    SELECT @guts = guts FROM @table2 where id = 1;
    SET @parent.modify('insert sql:variable("@guts") into (/Root[1])');

    INSERT @results (id, results) values (@id, @parent);

    FETCH NEXT FROM idCursor INTO @id

END 

CLOSE idCursor
DEALLOCATE idCursor

select * from @results;
Run Code Online (Sandbox Code Playgroud)