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

XQuery 在單個 SQL 更新命令中添加或替換屬性

XQuery adding or replacing attribute in single SQL update command(XQuery 在單個 SQL 更新命令中添加或替換屬性)
本文介紹了XQuery 在單個 SQL 更新命令中添加或替換屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個帶有 XML 列的表,如果屬性已經存在,我想更新 xml 以插入屬性或更改屬性值.

I have a Table with an XML column, I want to update the xml to insert attribute or to change the attribute value if the attribute already exists.

假設起始 xml 是:</>

Let's say the starting xml is: < d />

插入:

UPDATE Table 
set XmlCol.modify('insert attribute att {"1"} into /d[1]')

變化:

UPDATE Table
set XmlCol.modify('replace value of /d[1]/@att with "1"')

如果屬性已經存在,插入將失敗,如果屬性不存在,替換將失敗.我曾嘗試使用 'if',但我認為它行不通,出現錯誤:XQuery [modify()]:'attribute' 附近的語法錯誤,預期為 'else'."

insert will fail if the attribute already exists, replace will fail if the attribute doesn't exists. I have tried to use 'if' but I don't think it can work, there error I get: "XQuery [modify()]: Syntax error near 'attribute', expected 'else'."

IF 嘗試

UPDATE Table 
set XmlCol.modify('if empty(/d[1]/@att) 
                   then insert attribute att {"1"} into /d[1]
                   else replace value of /d[1]/@att with "1"')

目前我選擇 xml 到一個變量中,然后使用 T-SQL 修改它,然后用新的 xml 更新列,這需要我鎖定事務中的行,并且對于數據庫來說可能更昂貴.

Currently I select the xml into a variable and then modify it using T-SQL and then updating the column with new xml, this requires me to lock the row in a transaction and is probably more expensive for the DB.

推薦答案

據我所知,單條語句無法做到這一點.您可以使用 exist() 方法通過兩個更新語句來完成此操作.

From what I can tell, you can't do this with single statement. You can use the exist() method to accomplish that with two update statements.

DECLARE @TestTable TABLE
(
    Id int,
    XmlCol xml
);

INSERT INTO @TestTable (Id, XmlCol)
VALUES
    (1, '<d att="1" />'),
    (2, '<d />'),
    (3, '<d att="3" />');

SELECT * FROM @TestTable;

UPDATE @TestTable
SET XmlCol.modify('replace value of /d[1]/@att with "1"')
WHERE XmlCol.exist('(/d[1])[not(empty(@att))]') = 1;

UPDATE @TestTable
SET XmlCol.modify('insert attribute att {"1"} into /d[1]')
WHERE XmlCol.exist('(/d[1])[empty(@att)]') = 1;

SELECT * FROM @TestTable;

最終選擇的輸出是:

Id          XmlCol
----------- -------------------
1           <d att="1" />
2           <d att="1" />
3           <d att="1" />

這篇關于XQuery 在單個 SQL 更新命令中添加或替換屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲网站在线观看 | 日本三级网址 | 午夜精品久久久久久久久久久久久 | 精品成人佐山爱一区二区 | 亚洲在线免费观看 | 97精品国产97久久久久久免费 | 一级毛片在线视频 | 麻豆久久久久久久 | 自拍偷拍中文字幕 | 日韩精品视频一区二区三区 | 亚洲精品久久久久久久久久久 | 国产精品国产自产拍高清 | 国产在线一区观看 | 91五月婷蜜桃综合 | 成人免费在线观看 | 亚洲精品黄色 | 艹逼网 | 日韩国产免费观看 | 第四色影音先锋 | 久久6视频 | www.日韩av.com | 日韩精品在线一区 | 美女福利视频网站 | 亚洲成人免费av | 黑人性hd| 欧美福利精品 | 欧美影院 | 人人干超碰| 日本黄色免费片 | 国产欧美日韩一区 | 成年人免费网站 | 精品伦精品一区二区三区视频 | 亚洲美女一区 | 成人激情视频网 | 免费亚洲婷婷 | 精品久久久久久亚洲精品 | 国产玖玖 | 紧缚调教一区二区三区视频 | 日日日干干干 | 久久精品毛片 | 九九热在线免费视频 |