Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在T-SQL中合并XML?_Sql Server_Xml_Tsql - Fatal编程技术网

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

Sql server 如何在T-SQL中合并XML?,sql-server,xml,tsql,Sql Server,Xml,Tsql,看了这么多的文件似乎对我没什么帮助。考虑简化的例子: 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.paren

看了这么多的文件似乎对我没什么帮助。考虑简化的例子:

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
declare@table1表(id int,父xml)
插入@table1值(1,,)
声明@table2表(id int,guts xml)
插入@table2值(1,'hi mom!')
选择t1.parent.query(“”)
从t1.id=t2.id上的@table1 t1内部联接@table2 t2
将向查询函数传递什么来生成此结果

<Root><Guts>hi mom!</Guts></Root>
嗨,妈妈!

您要求的是XML操作,而不是关系操作。您想要的是通过向其中插入一段XML来生成一个新的XML,这意味着您必须使用该方法。从技术上讲,这是可能的,但是必须在更新上下文中调用modify(),因此它在SELECT中不起作用。它可以在集合或更新中工作:

UPDATE t1
 SET parent.modify(N'insert sql:column("t2.guts") into (/Root)[1]')
FROM @table1 t1
 JOIN @table2 t2 on t1.id = t2.id;
SELECT * from @table1;

如果必须在SELECT中获得结果,则必须将XML分解到关系表中,连接该关系表并使用FOR XML重新构造XML。

以下内容不是基于集合的,但可能会有所帮助(仅限SQL2008)

declare@table1表(id int,父xml)
插入@table1值(1,,)
声明@table2表(id int,guts xml)
插入@table2值(1,'hi mom!')
声明@id int;
声明@results表(id int,results xml);
声明的idCursor游标
从@table1中选择id
打开idCursor
从idCursor获取下一个到@id
而@@FETCH\u STATUS=0
开始
声明@parent xml,@guts xml
从id=1的@table1中选择@parent=parent;
从@table2中选择@guts=guts,其中id=1;
将@parent.modify('insert sql:variable(@guts))设置为(/Root[1]);
插入@results(id,results)值(@id,parent);
从idCursor获取下一个到@id
结束
关闭idCursor
取消分配idCursor
从@results中选择*;

这很好,因为我不需要使用游标。为什么SQL 2008是唯一的?对2005年也不起作用吗?顺便说一句,你需要在光标循环内的两个选择中使用@id。Remus,你当然是对的。应该使用@id。我说SQL 2008只是因为这里报告的问题:
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;