XQuery 在单个 SQL 更新命令中添加或替换属性

发布于 2024-09-25 15:49:52 字数 750 浏览 0 评论 0原文

我有一个带有 XML 列的表, 我想更新 xml 以插入属性或更改属性值(如果属性已存在)。

假设起始 xml 是: < 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’。”

如果尝试

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 更新列,这需要我在事务中锁定该行,并且对于数据库。

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.

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

Inserting:

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

Changing:

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

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 attempt

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独闯女儿国 2024-10-02 15:49:52

据我所知,你不能用单一语句来做到这一点。您可以使用 exist() 方法 通过两个更新语句来完成此操作。

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

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;

The output from the final select is:

Id          XmlCol
----------- -------------------
1           <d att="1" />
2           <d att="1" />
3           <d att="1" />
烟花肆意 2024-10-02 15:49:52

有一个比 Tommys 稍微好一点的方法:

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

INSERT INTO @TestTable (Id, XmlCol)
VALUES
    (1, '<UserSettings> </UserSettings>'),
    (2, '<UserSettings><timeout>3</timeout> </UserSettings>'),
    (3, '<UserSettings> </UserSettings>');

UPDATE @TestTable
SET XmlCol.modify('replace value of (/UserSettings/timeout/text())[1] with "1"')
WHERE Id = 3 and XmlCol.exist('/UserSettings/timeout') = 1;
IF @@ROWCOUNT=0
    UPDATE @TestTable
    SET XmlCol.modify('insert <timeout>5</timeout> into (/UserSettings)[1] ')
    WHERE Id = 3;

SELECT * FROM @TestTable;

解决方案是 Tommys 和简单 SQL 的组合,如果列存在,则只需要 1 个 SQL UPDATE。汤米总是需要两次更新。

There is a slightly better way than Tommys:

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

INSERT INTO @TestTable (Id, XmlCol)
VALUES
    (1, '<UserSettings> </UserSettings>'),
    (2, '<UserSettings><timeout>3</timeout> </UserSettings>'),
    (3, '<UserSettings> </UserSettings>');

UPDATE @TestTable
SET XmlCol.modify('replace value of (/UserSettings/timeout/text())[1] with "1"')
WHERE Id = 3 and XmlCol.exist('/UserSettings/timeout') = 1;
IF @@ROWCOUNT=0
    UPDATE @TestTable
    SET XmlCol.modify('insert <timeout>5</timeout> into (/UserSettings)[1] ')
    WHERE Id = 3;

SELECT * FROM @TestTable;

The solution is a combination of Tommys and simple SQL and required only 1 SQL UPDATE if the column exist. Tommys always recuire two updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文