<legend id='bokOp'><style id='bokOp'><dir id='bokOp'><q id='bokOp'></q></dir></style></legend>

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

      <tfoot id='bokOp'></tfoot>

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

      • <bdo id='bokOp'></bdo><ul id='bokOp'></ul>
      1. 如何根据该 XML 中的值更新 SQL 中的 XML

        时间:2023-06-07

            <small id='4LCpx'></small><noframes id='4LCpx'>

            • <bdo id='4LCpx'></bdo><ul id='4LCpx'></ul>

            • <tfoot id='4LCpx'></tfoot>

                <tbody id='4LCpx'></tbody>
                <i id='4LCpx'><tr id='4LCpx'><dt id='4LCpx'><q id='4LCpx'><span id='4LCpx'><b id='4LCpx'><form id='4LCpx'><ins id='4LCpx'></ins><ul id='4LCpx'></ul><sub id='4LCpx'></sub></form><legend id='4LCpx'></legend><bdo id='4LCpx'><pre id='4LCpx'><center id='4LCpx'></center></pre></bdo></b><th id='4LCpx'></th></span></q></dt></tr></i><div id='4LCpx'><tfoot id='4LCpx'></tfoot><dl id='4LCpx'><fieldset id='4LCpx'></fieldset></dl></div>
                • <legend id='4LCpx'><style id='4LCpx'><dir id='4LCpx'><q id='4LCpx'></q></dir></style></legend>
                • 本文介绍了如何根据该 XML 中的值更新 SQL 中的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我必须根据该 XML 的某些条件更新表的 XML.示例 XML:

                  I have to update XML of table based on some conditions of that XML. Sample XML:

                  <CountryValues>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>1</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>2</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>3</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>4</Month>
                      <PlaceValue>10</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Australia</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>1</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Australia</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>1</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Australia</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>1</Month>
                      <PlaceValue>4</PlaceValue>        
                    </CountryRow>
                   </CountryValues>
                  

                  每个国家/地区可以有多个地方.我必须根据 Country 和 Places 进行分组,然后我必须将 PlaceValues 更新为 null for PlaceValue = 0 除了 0 紧接在 PlaceValue > 1 之前.此示例中的示例,对于 Country = Brazil 和 PlaceName = 1,PlaceValue forMonth1 到 Month2 将为 Null 但 Month3 将保持为 0 作为其前一个 Month4 大于 0.

                  Each Country can have multiple Places. I have to group on the basis of Country and Places, then I have to update PlaceValues to null for PlaceValue = 0 except 0 which is immediately preceding PlaceValue > 1. Example in this sample, for Country = Brazil and PlaceName = 1, PlaceValue for Month1 to Month2 will be Null but Month3 will remain 0 as its preceding Month4 which is greate than 0.

                  推荐答案

                  基本上,我看到了两种处理方法.首先 - 将 xml 拆分为 sql 表/派生表,完成您的工作,然后再次组合成 xml.

                  Basically, I see 2 ways of dealing with this. First - split xml to sql table/derived table, do your work and then combine into xml again.

                  declare @data xml = 
                  '<CountryValues>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>1</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>2</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>3</Month>
                      <PlaceValue>0</PlaceValue>        
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>4</Month>
                      <PlaceValue>10</PlaceValue>        
                    </CountryRow>
                   </CountryValues>'
                  
                  ;with cte as (
                      select
                          t.c.value('CountryName[1]', 'nvarchar(max)') as CountryName,
                          t.c.value('PlaceName[1]', 'nvarchar(max)') as PlaceName,
                          t.c.value('Month[1]', 'int') as [Month],
                          t.c.value('PlaceValue[1]', 'int') as PlaceValue
                      from @data.nodes('CountryValues/CountryRow') as t(c)
                  )
                  select
                      c1.CountryName,
                      c1.PlaceName,
                      c1.[Month],
                      case
                          when c1.PlaceValue = 0 and isnull(c2.PlaceValue, 0) <= 1 then null
                          else c1.PlaceValue
                      end as PlaceValue
                  from cte as c1
                      left outer join cte as c2 on c2.CountryName = c1.CountryName and c2.PlaceName = c1.PlaceName and c2.[Month] = c1.[Month] + 1
                  for xml path('CountryRow'), root('CountryValues')
                  
                  ----------------------------------
                  <CountryValues>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>1</Month>
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>2</Month>
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>3</Month>
                      <PlaceValue>0</PlaceValue>
                    </CountryRow>
                    <CountryRow>
                      <CountryName>Brazil</CountryName>
                      <PlaceName>Place 1</PlaceName>
                      <Month>4</Month>
                      <PlaceValue>10</PlaceValue>
                    </CountryRow>
                  </CountryValues>
                  

                  第二种方法是在 xml 本身中使用 xquery.

                  Second way would be to use xquery inside the xml itself.

                  答案实际上取决于您所说的紧接在 PlaceValue > 1 之前"是什么意思.我在这里假设这意味着 - 值 > 1 的月份之前的月份.

                  The answer is really depends on what do you mean by "immediately preceding PlaceValue > 1". I've assumed here that this means - month right before month with value > 1.

                  这篇关于如何根据该 XML 中的值更新 SQL 中的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 SQL Server 2008 中读取 XML 列? 下一篇:在sql脚本中的两个XML文件之间交换数据?

                  相关文章

                  最新文章

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

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

                  1. <tfoot id='yVz9v'></tfoot>