gpt4 book ai didi

sql-server - 如何在 T-SQL 中合并 XML?

转载 作者:数据小太阳 更新时间:2023-10-29 02:49:31 24 4
gpt4 key购买 nike

似乎无论多读文档都对我没有帮助。考虑简化示例:

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

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

<Root><Guts>hi mom!</Guts></Root>

最佳答案

以下内容不是基于集合的,但也许会有所帮助(仅限 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;

关于sql-server - 如何在 T-SQL 中合并 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1828955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com