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 –