久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 精精国产视频 | 国产福利精品一区 | 日韩精品免费视频 | 久久精品国产清自在天天线 | 黄a在线观看 | 国产精品美女久久久久久不卡 | 看片国产 | 九九99久久| 一区二区三区av夏目彩春 | 天天艹 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 亚洲一区在线播放 | 欧美理论片在线观看 | 一级做a毛片 | 欧美精品一二区 | 中文字幕第一页在线 | 激情av免费看 | 精品1区 | 久草资源 | 国产精品久久国产愉拍 | 一区二区免费在线观看 | 亚洲精品一区二区久 | 日本不卡高字幕在线2019 | 精品久久久久久久 | 日本aa毛片a级毛片免费观看 | 欧美精品一区二区三区在线播放 | 97人人澡人人爽91综合色 | 香蕉视频久久久 | 最新午夜综合福利视频 | 亚洲在线高清 | 成人av高清| 国产精品九九九 | 亚洲成人中文字幕 | 一区二区三区视频在线观看 | 日韩在线播放av | 欧美视频免费在线 | 网色| 国内自拍第一页 | 欧美男人天堂 | 精品中文在线 | 午夜在线 |