2013-06-03 81 views
3

我有一個包含字段id(int)和XmlField(xml)的表(讓我們稱她爲t)。在單個查詢中插入多個節點到xml字段

我嘗試在一個查詢中添加多個節點,但無論我嘗試了什麼,我總是收到錯誤。

查詢是:

update t 
set XmlField.modify('insert <f1>value here</f1><f2>value there</f2> into (/xmldoc)') 

,我得到的錯誤:

XQuery [t.XmlField.modify()]: Syntax error near '', expected 'as', 'into', 'before' or 'after'.

當我試圖只添加一個XML節點它的工作(例如):

update t set XmlField.modify('insert <f1>value here</f1> into (/xmldoc)') 

當我嘗試添加像這樣的嵌套節點時,它也在工作:

update t set XmlField.modify('insert <f><f1>value here</f1><f2>value there</f2></f> into (/xmldoc)') 

有什麼辦法可以實現它嗎?

回答

3

SQL Server documentation的確說得很清楚,insert語句可以處理多個節點。所以我的猜測是你的問題只是一個語法錯誤。 (微軟的語法與XQuery Update Facility spec中定義的略有不同,但它是相當類似的。)

我試着將元素f1和f2變成一個序列並用圓括號包裝它們(規範要求在這裏有一個ExprSingle,意味着沒有頂級逗號允許):

update t 
set XmlField.modify(
    'insert (<f1>value here</f1>, <f2>value there</f2>) into (/xmldoc)') 

(針對SQL Server未測試)

+0

謝謝!這就解決了問題! – David