<tfoot id='N5Ca5'></tfoot>
<i id='N5Ca5'><tr id='N5Ca5'><dt id='N5Ca5'><q id='N5Ca5'><span id='N5Ca5'><b id='N5Ca5'><form id='N5Ca5'><ins id='N5Ca5'></ins><ul id='N5Ca5'></ul><sub id='N5Ca5'></sub></form><legend id='N5Ca5'></legend><bdo id='N5Ca5'><pre id='N5Ca5'><center id='N5Ca5'></center></pre></bdo></b><th id='N5Ca5'></th></span></q></dt></tr></i><div id='N5Ca5'><tfoot id='N5Ca5'></tfoot><dl id='N5Ca5'><fieldset id='N5Ca5'></fieldset></dl></div>

<small id='N5Ca5'></small><noframes id='N5Ca5'>

  • <legend id='N5Ca5'><style id='N5Ca5'><dir id='N5Ca5'><q id='N5Ca5'></q></dir></style></legend>

        • <bdo id='N5Ca5'></bdo><ul id='N5Ca5'></ul>

        使用 SQL 在 XML 中插入节点

        时间:2023-06-07
        • <bdo id='VcyRU'></bdo><ul id='VcyRU'></ul>
          <tfoot id='VcyRU'></tfoot>
          <i id='VcyRU'><tr id='VcyRU'><dt id='VcyRU'><q id='VcyRU'><span id='VcyRU'><b id='VcyRU'><form id='VcyRU'><ins id='VcyRU'></ins><ul id='VcyRU'></ul><sub id='VcyRU'></sub></form><legend id='VcyRU'></legend><bdo id='VcyRU'><pre id='VcyRU'><center id='VcyRU'></center></pre></bdo></b><th id='VcyRU'></th></span></q></dt></tr></i><div id='VcyRU'><tfoot id='VcyRU'></tfoot><dl id='VcyRU'><fieldset id='VcyRU'></fieldset></dl></div>

                <tbody id='VcyRU'></tbody>

                <small id='VcyRU'></small><noframes id='VcyRU'>

                <legend id='VcyRU'><style id='VcyRU'><dir id='VcyRU'><q id='VcyRU'></q></dir></style></legend>
                  本文介绍了使用 SQL 在 XML 中插入节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有以下 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 firstas lastbefore/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]');

                  这将创建一个新节点 content</type>,其中的内容从变量 @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模板网!

                  上一篇:使用取自序列的值将属性添加到 xmltype 下一篇:如何使用 for 循环使用 XQuery 删除 xml 节点

                  相关文章

                  最新文章

                  <small id='D0Ddp'></small><noframes id='D0Ddp'>

                  <legend id='D0Ddp'><style id='D0Ddp'><dir id='D0Ddp'><q id='D0Ddp'></q></dir></style></legend>
                    <bdo id='D0Ddp'></bdo><ul id='D0Ddp'></ul>

                  1. <tfoot id='D0Ddp'></tfoot>
                  2. <i id='D0Ddp'><tr id='D0Ddp'><dt id='D0Ddp'><q id='D0Ddp'><span id='D0Ddp'><b id='D0Ddp'><form id='D0Ddp'><ins id='D0Ddp'></ins><ul id='D0Ddp'></ul><sub id='D0Ddp'></sub></form><legend id='D0Ddp'></legend><bdo id='D0Ddp'><pre id='D0Ddp'><center id='D0Ddp'></center></pre></bdo></b><th id='D0Ddp'></th></span></q></dt></tr></i><div id='D0Ddp'><tfoot id='D0Ddp'></tfoot><dl id='D0Ddp'><fieldset id='D0Ddp'></fieldset></dl></div>