問題描述
我正在嘗試根據(jù)大小寫更新表中的 XML 數(shù)據(jù)列.下面是我的代碼:
I am trying to update XML data column in a table based on case. Below is my code:
UPDATE #temp
SET xml_data = CASE
WHEN @type = 'G'
THEN xml_data.modify('insert <type>G</type> after (/Main/name)[1]');
WHEN @type = 'Q'
THEN xml_data.modify('insert <type>Q</type> after (/Main/name)[1]');
END
我收到一個(gè)錯(cuò)誤:
XML 數(shù)據(jù)類型方法修改"的錯(cuò)誤使用.在這種情況下需要一個(gè)非增變方法.
Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.
期望的輸出:
- 當(dāng)
@type = 'Q'
時(shí),插入類型節(jié)點(diǎn)為Q
- 當(dāng)
@type = 'G'
時(shí),插入類型節(jié)點(diǎn)為G
- When
@type = 'Q'
, insert type node asQ
- When
@type = 'G'
, insert type node asG
XML 結(jié)構(gòu):
<Main>
<name>John doe</name>
<type>Q</type>
<age>15</age>
</Main>
有什么幫助嗎?!
更新:
我編輯的查詢:
UPDATE #temp
SET xml_data.modify('insert <Type>{sql:variable("@var")}</Type> after (/Main/name)[1]')
此查詢將類型添加到 XML 的末尾.輸出:
This query is adding the type to the end of the XML. Output:
<Main>
<name>John doe</name>
<age>15</age>
</Main>
<Type>Q</Type>
推薦答案
語法是SET [xml_column].modify
,沒有使用賦值.不使用 CASE
,而是使用特殊的 sql:variable
函數(shù)將變量折疊到更新中:
The syntax is SET [xml_column].modify
, no use of the assignment. Instead of using CASE
, fold the variable into the update using the special sql:variable
function:
UPDATE #temp
SET xml_data.modify('insert <type>{sql:variable("@type")}</type> after (/Main/name)[1]');
這篇關(guān)于XML 數(shù)據(jù)類型方法“修改"的錯(cuò)誤使用.在這種情況下需要一個(gè)非突變方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!