1
这是为什么这样工作?SQL Server XML字段 - 使用TSQL变量作为XML节点序列指示器
SELECT
XDocument
.value('(/Book/Chapter[@verse="allo"])[2]',
nvarchar(max)')
FROM XBiblos
WHERE Version = 666
但是为什么这不是工作?其中唯一的差异是[2]
由[sql:variable("@i")]
导致错误味精 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
DECLARE @i int;
SET @i = 2;
SELECT
XDocument
.value('(/Book/Chapter[@verse="allo"])[sql:variable("@i")]',
nvarchar(max)')
FROM XBiblos
WHERE Version = 666
文档结构例子...
<XDocument name="DraXQueLah">
<Book name="Worldymort">
<Chapter verse="Forgot">Forgot so loved the world</Chapter >
<Chapter verse="Icecream">That we were eternally creamed</Chapter >
<Chapter verse="blah">blah blah</Chapter >
<Chapter verse="blah">blah blah</Chapter >
<Chapter verse="blah">blah blah</Chapter >
<Chapter verse="blah blah">blah blah blah</Chapter >
</Book>
</XDocument>
jd,那就是如何使用sql:variable()函数。阅读https://msdn.microsoft.com/en-us/library/ms188254.aspx –