[英]SQL Server 2008 & XQUERY
I'm currently using Xquery in SQL Server to extract some information from XML. 我目前在SQL Server中使用Xquery从XML中提取一些信息。
I'm having troubles because I have to put some dynamics content on one variable but it doesn't work. 我遇到了麻烦,因为我必须将一些动态内容放在一个变量上,但是它不起作用。
When I select this : 当我选择这个时:
P.value('ListOrderItem[1]/OrderItem[1]/Item[sql:variable("@I")]/Seller[1]','VARCHAR(64)')
P
is my path and it's good because it's working with other items, but I have to do a loop on this item (in one order, you can have many items...), so that's why I want to put the @I
and then do a loop on this variable. P
是我的路径,它很好,因为它可以与其他项目一起使用,但是我必须对此项目执行循环(以一个顺序,您可以有很多项目...),所以这就是为什么我要使用@I
和然后对此变量执行循环。
PS: Don't say to put the [1]
after the variable, else it will select the first item every time. PS:不要说将[1]
放在变量之后,否则每次都会选择第一项。
Edit: By "doesn't work" i mean that it send me only the first item when i put the [1]
and error message when i put " [sql:variable("@I")]
" which is "value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *" 编辑:“不起作用”是指当我放入[1]
时,它仅向我发送第一项,而当我放入“ [sql:variable("@I")]
”(即“ value( )'需要一个单例(或空序列),找到的类型为'xdt:untypedAtomic *“的操作数
And of course, i've tried the '+@I'
but it still doesn't work.. 当然,我已经尝试过'+@I'
但它仍然无法正常工作。
Example of my XML : 我的XML示例:
<ListOrderItem>
<OrderItem>
<Item>
...
</Item>
<Item>
...
</Item>
</OrderItem>
</ListOrderItem>
Final edit : I finally fixed the problem this post helped me a lot, sorry for posting then. 最终编辑:我终于解决了此帖子对我有很大帮助的问题,很抱歉当时发布了该帖子。
Getting multiple records from xml column with value() in SQL Server 在SQL Server中使用value()从xml列中获取多个记录
Thanks. 谢谢。
Try this... 尝试这个...
select v.x.value('.','varchar(20)')
from
p.nodes('ListOrderItem/OrderItem//Seller[1]') v(x)
Embed your entire expression in parenthesis and add the [1]
to the end. 将整个表达式嵌入括号中,然后将[1]
添加到末尾。
declare @XML xml = '
<ListOrderItem>
<OrderItem>
<Item>
<Seller>1</Seller>
</Item>
<Item>
<Seller>2</Seller>
</Item>
</OrderItem>
</ListOrderItem>'
declare @I int = 2
select @XML.value('(ListOrderItem[1]/OrderItem[1]/Item[sql:variable("@I")]/Seller[1])[1]','VARCHAR(64)')
Result: 2 结果:2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.