Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:45:53+00:00 2026-05-16T06:45:53+00:00

What would the XQuery look like to check if a node exists, and if

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:45:53+00:00Added an answer on May 16, 2026 at 6:45 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read the xsi:type attribute from the current node in a
I would like to convert a string into a node. I have a method
Would you mind to take a look at my jsfiddle ? As you can
Would a Python script like this be safe to use? There would be a
Would it not make sense to support a set of languages (Java, Python, Ruby,
Would the following SQL remove also the index - or does it have to
Would having a nice little feature that makes it quicker to write code like
I have an external variable coming in as a string and I would like
I would like to understand why on .NET there are nine integer types: Char
I work with XQuery to do statistics. I have one document like this :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.