久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL Server - 僅使用 .modify() 合并兩個 XML

SQL Server - merge two XML using only .modify()(SQL Server - 僅使用 .modify() 合并兩個 XML)
本文介紹了SQL Server - 僅使用 .modify() 合并兩個 XML的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我們有:

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>';

游樂場

現在我想根據 id 屬性合并 2 個 XML:

Now I want to merge 2 XMLs based on id attribute:

id = 1 的用戶的最終結果:

Final result for user with 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>

請注意,它不是組合 2 個 XML,而是upsert"操作.

Please note that it is not combining 2 XMLs but "upsert" operation.

備注:

  • 我知道這種模式違反了數據庫規范化,規范化是可行的方法(但在這種情況下不是)
  • 我知道使用派生表的解決方案,.nodes().value() 函數首先解析兩個 XML,然后合并并寫回
  • I know that this kind of schema violates database normalization and normalizing it is the way to go (but not in this case)
  • I know solution that utilize derived tables, .nodes() and .value() functions first to parse both XML, then merge and write back

我正在尋找的是 XPath/XQuery 表達式,它將合并到一個語句中(無派生表/動態 sql*):

I am searching for is XPath/XQuery expression that will merge it in one statement (no derived tables/dynamic-sql*):

* 如果絕對需要,可以使用動態 SQL,但我想避免使用.

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


/* replace ... for ... where ... with sql:variable */

推薦答案

嘗試了一段時間后,我認為這是不可能的...

After trying around a while I think this is not possible...

這里有類似的問題:XQuery 在單個 SQL 更新命令中添加或替換屬性

.modify(insert Expression1 ... ) 不允許在通過 @sql:variable() 傳入的 XML 獲取數據code> 或 sql:column()

The .modify(insert Expression1 ... ) does not allow to get data within an XML passed in via @sql:variable() or sql:column()

在此處閱讀:https://msdn.microsoft.com/en-us/library/ms175466.aspx 在 Expression1 -> "常量 XML 或獨立的 sql:column/sql:variable 或 XQuery(到同一實例)

Read here: https://msdn.microsoft.com/en-us/library/ms175466.aspx at Expression1 -> "constant XML or stand alone sql:column / sql:variable or XQuery (to the same instance)

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>
*/

您可以這樣聲明第二個 XML:

You might declare the second XML as such:

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" />';

但是您將沒有機會使用 id 的值作為 XQuery 過濾器

But than you'll have no chance to use the id's value as XQuery filter

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

最后但并非最不重要的一點,我認為沒有機會在 .modify() 的一次調用中組合兩個不同的 XML_DML 語句.

And last but not least I think there is no chance to combine two different XML_DML statements within one call of .modify().

我唯一的想法就是這個,但它行不通.IF 似乎只能在一個表達式中使用,但不能區分兩個執行路徑

The only idea I had was this, but it doesn't work. IF seems to be usable only within an Expression, but not two distinguish between two execution paths

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');

所以我的結論是:不,這是不可能的...

So my conclusion: No, this is not possible...

我在這里提供的解決方案 https://stackoverflow.com/a/35060150/5089204 在第二部分(如果你想‘合并’兩本書結構")將是我解決這個問題的方法.

The solution I provided here https://stackoverflow.com/a/35060150/5089204 in the second section ("If you want to 'merge' two Books-structures") would be my way to solve this.

這篇關于SQL Server - 僅使用 .modify() 合并兩個 XML的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产成人久久av免费高清密臂 | 亚洲国产精品久久久久 | 在线一区视频 | 青青草av在线播放 | 国产一区二区欧美 | 精品久久久久久一区二区 | 欧美一级免费片 | 亚洲 欧美 日韩 在线 | 欧美成视频 | 久久国产精品一区二区三区 | av网站在线免费观看 | 亚洲网站在线观看 | 精品91久久 | 亚洲一区亚洲二区 | 婷婷五月色综合 | 欧美久久天堂 | 免费三级网站 | 国产日韩精品一区二区 | 香蕉视频一区二区 | 久久久久免费精品国产 | 久久久久国产精品一区二区 | 成人精品免费 | 亚洲国产精品成人综合久久久 | 日本黄色大片免费 | 欧美一级片a | 免费在线视频一区二区 | 国产精品一区一区三区 | 免费a网 | 国产成人精品免费视频 | 精品九九| 亚洲www啪成人一区二区麻豆 | 成人免费观看视频 | 久久人人网 | 国产成人精品免高潮在线观看 | 国产精品久久久久久久久久久久 | 成人h视频 | 久久久久久国产精品久久 | 男人av网 | 欧美日韩免费在线 | 九九亚洲 | 国产精品一区二区三区四区 |