0

I have a column in my SQL Server table with this XML value :

<ValidTestResult version="1.0" encoding="UTF-8">
  <item CarType="پراید" SystemType="2" model="1382">
    <count>7777</count>
  </item>
  <item CarType="1" SystemType="2" model="1382">
    <count>100</count>
  </item>
  <item CarType="1" SystemType="2" model="1380">
    <count>100</count>
  </item>
</ValidTestResult>

I want to check if a node exists, update the value of count. I do that using this code:

UPDATE InquiryStatus
SET TotalCarburetorResult.modify(N'replace value of 
(/ValidTestResult/item[@model=("1382") and @CarType=("پراید")]/count/text())[1] with ("7777")') where id=27
GO

UPDATE InquiryStatus
SET TotalCarburetorResult.modify(N'insert <item CarType="پراید" SystemType="2" model="1382"><count>100</count></item> into (/Result)[1]')

As you can see my code inserts and updates the value, but I need something else: I want to check if the node exists, then update the value count and if doesn't exist, it should insert a new node. How can I check if the node exists?

For example a node with this attributes:

<item CarType="1" SystemType="2" model="1382">
2
  • Do you want to check on all three attributes? Or does one of them suffice? Commented Oct 29, 2017 at 15:44
  • @marc_s Yes exactly Commented Oct 29, 2017 at 15:53

1 Answer 1

1
SELECT TotalCarburetorResult.exist(N'/ValidTestResult/item[@model=("1380") and @CarType=("پراید")]') 
FROM InquiryStatus 
WHERE id = 27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.