SQL Server - 仅使用.modify()合并两个XML

Luk*_*zda 9 xml sql t-sql sql-server xpath

假设我们有:

CREATE TABLE #Users(id INT PRIMARY KEY, name VARCHAR(100), suggestions XML);

INSERT INTO #Users(id, name, suggestions)
SELECT 1, 'Bob', N'<Products>
                     <Product id="1" score="1"/>
                     <Product id="2" score="5"/>
                     <Product id="3" score="4"/>
                   </Products>'
UNION ALL
SELECT 2, 'Jimmy', N'<Products>
                       <Product id="6" score="3"/>
                     </Products>';

DECLARE @userId INT = 1,
        @suggestions XML = N'<Products>
                              <Product id="2" score="5"/>
                              <Product id="3" score="2"/>
                              <Product id="7" score="1" />
                             </Products>';
Run Code Online (Sandbox Code Playgroud)

Playground

现在我想根据id属性合并2个XML :

id = 1的用户的最终结果:

<Products>
  <Product id="1" score="1"/> -- nothing changed (but not exists in @suggestions)
  <Product id="2" score="5"/> -- nothing changed (but exists in @suggestions)
  <Product id="3" score="2"/> -- update score to 2
  <Product id="7" score="1"/> -- insert new element
</Products>
Run Code Online (Sandbox Code Playgroud)

请注意,它不是组合2个XML而是"upsert"操作.

备注:

  • 我知道这种模式违反了数据库规范化并规范化它是要走的路(但不是在这种情况下)
  • 我知道利用派生表的解决方案,.nodes()并且.value()函数首先解析XML,然后合并并写回

我正在搜索XPath/XQuery将在一个语句中合并它的表达式(没有派生表/ dynamic-sql*):

*如果绝对需要,可以使用动态SQL,但我想避免它.

UPDATE #Users
SET suggestions.modify(... sql:variable("@suggestions") ...); --changes only here
WHERE id = @userId;


/* replace ... for ... where ... with sql:variable */
Run Code Online (Sandbox Code Playgroud)

Shn*_*ugo 4

经过一段时间的尝试,我认为这是不可能的......

这里有类似的问题:XQuery在单个SQL更新命令中添加或替换属性

不允许.modify(insert Expression1 ... )获取通过或传入的 XML中的数据@sql:variable()sql:column()

请阅读此处:https: //msdn.microsoft.com/en-us/library/ms175466.aspx,位于 Expression1 ->“常量 XML 或独立 sql:column / sql:variable 或 XQuery(到同一实例)

DECLARE @xml1 XML= --the existing XML
'<Products>
  <Product id="1" score="1" />
  <Product id="2" score="5" />
  <Product id="3" score="4" />
</Products>';

DECLARE @xml2 XML= --the XML with new or changed data
'<Products>
  <Product id="2" score="5" />
  <Product id="3" score="2" />
  <Product id="7" score="1" />
</Products>';

SET @xml1.modify('insert sql:variable("@xml2") as first into /Products[1]');

SELECT @xml1;

/* The full node is inserted!
Without any kind of preparation there is NO CHANCE to get the inner nodes only

<Products>
  <Products>
    <Product id="2" score="5" />
    <Product id="3" score="2" />
    <Product id="7" score="1" />
  </Products>
  <Product id="1" score="1" />
  <Product id="2" score="5" />
  <Product id="3" score="4" />
</Products>
*/
Run Code Online (Sandbox Code Playgroud)

您可以这样声明第二个 XML:

DECLARE @xml2 XML= --the XML with new or changed data
'<Product id="2" score="5" />
 <Product id="3" score="2" />
 <Product id="7" score="1" />';
Run Code Online (Sandbox Code Playgroud)

但是您将没有机会使用 id 的值作为 XQuery 过滤器

SET @xml1.modify('insert sql:variable("@xml2") as first into /Products[**How should one filter here?**]');
Run Code Online (Sandbox Code Playgroud)

最后但并非最不重要的一点是,我认为没有机会在一次调用中组合两个不同的 XML_DML 语句.modify()

我唯一的想法就是这个,但行不通。IF似乎只能在表达式使用,但不能区分两个执行路径

SET @xml1.modify('if (1=1) then
                     insert sql:variable("@xml2") as first into /Products[1]
                  else
                     replace value of /Products[1]/Product[@id=1][1]/@score with 100');
Run Code Online (Sandbox Code Playgroud)

所以我的结论是:不,这是不可能的......

我在第二部分(“如果你想‘合并’两本书结构”)中提供的解决方案/sf/answers/2454210531/将是我解决这个问题的方法。