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 a brief and easy way to strip tags from an XHTML
Here is a description of the scenario and I would appreciate also any comments
Would like a button in my widget to fire the APPWIDGET_UPDATE intent on the
would like to find all the nearby places for a given place or address.
Would anyone happen to know how you would run a Twitter search query based
I'm a little bit confused concerning variable updates in XQuery: On [1] it says:
Would some kind person help me sort out the output of .Net Reflector v6.5
Would you consider extending the native elements via the prototype dangerous? I see some
I have created a table that has a strongly type XML column in SQL2008
I am trying to figure out how I can load my table variable with

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.