XQuery if 存在条件插入/替换

发布于 2024-09-10 17:38:29 字数 745 浏览 1 评论 0原文

XQuery 会是什么样子来检查节点是否存在,如果存在则运行替换语句,如果不存在则运行插入语句?

这就是我的想法。我想存储用户是否已阅读 XML 中的重要消息。数据如下所示。

<usersettings>
    <message haveRead="0" messageId="23" ></message>
    <message haveRead="1" messageId="22" ></message>
</usersettings>

基本上,此 XML 告诉我用户已阅读一条消息,而另一条消息仍需要查看/读取。

我想将插入/替换 xquery 合并到一个语句中。这就是我的想法。

UPDATE WebUsers SET UserSettings.modify('

        declare default element namespace "http://www.test.com/test"; 

        IF a node exists with the messageId
            code to replace node with new update
        ELSE
            code to insert a new node with the provided variables
        ')

        WHERE Id = @WebUserId

What would the XQuery look like to check if a node exists, and if it does then run a replace statement, if not then an insert statement?

Here's what I have in mind. I want to store whether or not a user has read an important message in XML. Here's what the data would look like.

<usersettings>
    <message haveRead="0" messageId="23" ></message>
    <message haveRead="1" messageId="22" ></message>
</usersettings>

Basically this XML tells me that the user has read one message, while the other message still needs to be viewed / read.

I want to combine my insert / replace xquery into one statement. Here's what I had in mind.

UPDATE WebUsers SET UserSettings.modify('

        declare default element namespace "http://www.test.com/test"; 

        IF a node exists with the messageId
            code to replace node with new update
        ELSE
            code to insert a new node with the provided variables
        ')

        WHERE Id = @WebUserId

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

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

发布评论

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

评论(1

帅冕 2024-09-17 17:38:29

我还没有找到一种真正令人满意的方法来做到这一点,但其中一种可能适合您。我更喜欢第一种技术,但我很恼火的是我还没有找到更优雅的方法。

首先确保节点始终存在

DECLARE @messageID int;
SET @messageID=24;

DECLARE @myDoc xml;
SET @myDoc = 
'<usersettings>
    <message haveRead="0" messageId="23" >msg</message>
    <message haveRead="1" messageId="22" >msg</message>
</usersettings>';
SELECT @myDoc;


SET @myDoc.modify('
    insert
    if (count(//message[@messageId=sql:variable("@messageID")]) = 0)
    then <message haveRead="0">new msg</message>
    else()
         as last into (/usersettings)[1]
');

SELECT @myDoc;

--now do the rest, safe that the node exists

切换

DECLARE @myDoc xml;
SET @myDoc = 
'<usersettings>
    <message haveRead="0" messageId="23" >msg</message>
    <message haveRead="1" messageId="22" >msg</message>
</usersettings>';
SELECT @myDoc;

DECLARE @messageID int;
SET @messageID=23;

IF @myDoc.exist('//message[@messageId=sql:variable("@messageID")]') = 1
BEGIN
    SET @myDoc.modify('replace value of (//message[@messageId=sql:variable("@messageID")]/text())[1]
                       with "test"')
END
ELSE
BEGIN
    SET @myDoc.modify('insert <message haveRead="0">new msg</message>
                       into (/usersettings)[1]')
END

SELECT @myDoc;

I haven't found a really satisfactory way of doing this, but one of these might work for you. I like the first technique better, but I'm annoyed that I haven't found a more elegant way of doing it.

Make sure the node always exists first

DECLARE @messageID int;
SET @messageID=24;

DECLARE @myDoc xml;
SET @myDoc = 
'<usersettings>
    <message haveRead="0" messageId="23" >msg</message>
    <message haveRead="1" messageId="22" >msg</message>
</usersettings>';
SELECT @myDoc;


SET @myDoc.modify('
    insert
    if (count(//message[@messageId=sql:variable("@messageID")]) = 0)
    then <message haveRead="0">new msg</message>
    else()
         as last into (/usersettings)[1]
');

SELECT @myDoc;

--now do the rest, safe that the node exists

Switching

DECLARE @myDoc xml;
SET @myDoc = 
'<usersettings>
    <message haveRead="0" messageId="23" >msg</message>
    <message haveRead="1" messageId="22" >msg</message>
</usersettings>';
SELECT @myDoc;

DECLARE @messageID int;
SET @messageID=23;

IF @myDoc.exist('//message[@messageId=sql:variable("@messageID")]') = 1
BEGIN
    SET @myDoc.modify('replace value of (//message[@messageId=sql:variable("@messageID")]/text())[1]
                       with "test"')
END
ELSE
BEGIN
    SET @myDoc.modify('insert <message haveRead="0">new msg</message>
                       into (/usersettings)[1]')
END

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