問題描述
我有以下 XML:
創建表#temp(cid int,xml_data xml)插入 cid值(1001,'<主><name>''John doe''</name><年齡>15</年齡></主要>')
我想根據一個簡單的參數條件向這個 XML 添加一個額外的節點:
所需的輸出:
<name>John doe</name><type>Q</type><年齡>15</年齡></主要>
代碼:
select case when @type = 'Q' then更新#tempSET Main.modify('insert into(/主要的)')走
我收到語法錯誤.任何幫助!
更新:
我在代碼中實施了建議的解決方案,但出現以下錯誤.錯過了一些愚蠢的東西!
更新 #temp設置 xml_data =案件當@type = 'G'然后 xml_data.modify('insert G into (/Main)[1]');當@type = 'Q'然后 xml_data.modify('insert Q into (/Main)[1]');結尾
我收到'XML 數據類型方法'修改'的錯誤使用.在這種情況下需要一個非突變方法.錯誤
不需要任何復雜的麻煩.只需根據需要插入所需的節點:
UPDATE #temp SET xml_data.modify('insert Q into (/Main)[1]');
使用 as first
、as last
或 before/after
允許您指定節點的位置.下面將新節點直接放在
之后:
UPDATE #temp SET xml_data.modify('insert Q after (/Main/name)[1]');
更新您關于更新語句的問題
你的陳述有幾個缺陷:
<塊引用>更新#temp設置 xml_data =案件當@type = 'G'然后 xml_data.modify('insert G into (/Main)[1]');當@type = 'Q'然后 xml_data.modify('insert Q into (/Main)[1]');結尾
您不能使用語法 SET xmlColumn = xmlColumn.modify()
.您必須使用 SET xmlColumn.modify()
,而且分號無論如何都會破壞這一點.
老實說,我覺得這很復雜,試試這個:
DECLARE @type VARCHAR(1)='Q'UPDATE #temp SET xml_data.modify('insert {sql:variable("@type")} into (/Main)[1]');
這將創建一個新節點
,其中的內容從變量 @type
中取出.
I have the below XML:
create table #temp(cid int,xml_data xml)
insert into cid
values(1001,
'<Main>
<name>''John doe''</name>
<age>15</age>
</Main>')
I want to add an additional node to this XML based on a simple parametric condition:
desired output:
<Main>
<name>John doe</name>
<type>Q</type>
<age>15</age>
</Main>
code:
select case when @type = 'Q' then
UPDATE #temp
SET Main.modify('insert <type = 'Q'> into
(/Main)')
GO
I am getting syntax error. Any help!
UPDATE:
I implemented the suggested solution in my code and I'm getting below error. Missing out something silly!
UPDATE #temp
SET xml_data =
case
when @type = 'G'
then xml_data.modify('insert <type>G</type> into (/Main)[1]');
when @type = 'Q'
then xml_data.modify('insert <type>Q</type> into (/Main)[1]'); end
I am getting 'Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.' error
No need for any complicated hassel. Just insert the node you want as you want it:
UPDATE #temp SET xml_data.modify('insert <type>Q</type> into (/Main)[1]');
Using as first
, as last
or before / after
allows you to specify the node's position. The following will place the new node directly after <name>
:
UPDATE #temp SET xml_data.modify('insert <type>Q</type> after (/Main/name)[1]');
UPDATE Your question about an update-statement
Your statement has several flaws:
UPDATE #temp SET xml_data = case when @type = 'G' then xml_data.modify('insert <type>G</type> into (/Main)[1]'); when @type = 'Q' then xml_data.modify('insert <type>Q</type> into (/Main)[1]'); end
You cannot use the syntax SET xmlColumn = xmlColumn.modify()
. You have to use SET xmlColumn.modify()
, Furthermore the semicolons are breaking this anyway.
To be honest, I think this is to complicated, try this:
DECLARE @type VARCHAR(1)='Q'
UPDATE #temp SET xml_data.modify('insert <type>{sql:variable("@type")}</type> into (/Main)[1]');
This will create a new node <type>content</type>
, with a content taken out ot the variable @type
.
這篇關于使用 SQL 在 XML 中插入節點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!