在元素级别分割t-sql @xml变量的游标 [英] Cursor for spliting t-sql @xml variable on elements level
问题描述
我需要定义一些游标,以便将元素级别的t-sql @xml变量拆分为不同的@xml.
I need to define some cursor for spliting t-sql @xml variable on elements level into different @xml(s).
例如:
<root>
<element id=10/>
<element id=11/>
<element id=12/>
<element id=13/>
</root>
以便在tsql游标内获取以下值:
so that get the following values inside of tsql cursor:
< root>< element id = 10/>< element id = 11/></root>
然后
< root>< element id = 12/>< element id = 13/></root>
依此类推,其中n个元素在光标循环之前.
and so on where n number of elements pro cursor loop.
推荐答案
好,您可以使用内置函数来操作 XML
.例如,以下语句:
Well, you can use the build-in functions for manipulating XML
. For example, the following statement:
DECLARE @XML XML = N'<root><element id="10"/><element id="11"/><element id="12"/><element id="13"/></root>'
SELECT ROW_NUMBER() OVER (ORDER BY T.c)
,T.c.query('.')
FROM @XML.nodes('root/element') T(c)
将为您提供所有 elements
元素,并保留它们在 XML
结构中的顺序:
will give you all elements
preserving the order they have in the XML
structure:
然后,您可以存储此结果并构建单独的较小的 XML
变量.
Then you can stored this result and build separate smaller XML
variables.
对于不同的元素,您可以像这样使用 *
:
For different elements you can use *
like this:
DECLARE @XML XML = N'<root><element1 id="10"/><element2 id="11"/><element3 id="12"/><element4 id="13"/></root>'
SELECT ROW_NUMBER() OVER (ORDER BY T.c)
,T.c.query('.')
FROM @XML.nodes('root/*') T(c)
这篇关于在元素级别分割t-sql @xml变量的游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!