2

I went through all the various question pertaining to this topic, and I cant seem to find an answer in what is already provided.

What I am trying to do is insert a new node in the xml structure. When I run the below code, it doesnt shoot an error, but the XML doesn't not contain the newly inserted "material" sub section.

Would any of you mind pointing out where I am messing up here?

 DECLARE @t TABLE (rowID int IDENTITY PRIMARY KEY, XmlData XML)

 INSERT INTO @t (XmlData)
VALUES ('
<Root>
<ProductDescription ProductID="01" ProductName="Widget">
    <Features>
        <Warranty>1 year</Warranty>
        <Maintenance>Monthly</Maintenance>
    </Features>
</ProductDescription>
</Root>
')

UPDATE @t
SET XmlData.modify('insert <Material /> as first into (/Features)[1]')

SELECT 'before' s, DATALENGTH(XmlData) dl, XmlData
FROM @t
WHERE rowId = 1

1 Answer 1

2

You need to specify the complete XPath to the location where you want to insert your new XML fragment:

UPDATE @t
SET XmlData.modify('insert <Material /> as first into (/Root/ProductDescription/Features)[1]')

You need to use

/Root/ProductDescription/Features)[1]

not just

/Features)[1]

then it'll work

Sign up to request clarification or add additional context in comments.

1 Comment

You rock. Thanks. I was like "Why isnt this working "

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.