1

Using Microsoft SQL Server Management Studio, I've inserted an element into the xml file and now wish to insert an attribute so it looks like:

<a><b c="2"/></a>

I created b with:

update #tmp set column.modify('insert <b/> into (/a)[1]')

Now I wish to create c as an attribute of b as shown above. So I try:

update #tmp set column.modify('insert attribute c{''2''} into (/a/@b)[1]')

But I get the error:

XQuery [#tmp.column.modify()]: The target of 'insert into' must be an element/document node, found 'attribute(b,xdt:untypedAtomic)

1 Answer 1

1

b is an element, not an attribute, so you'll need to drop the @:

update #tmp
  set [column].modify('insert attribute c{''2''} into (/a/b)[1]');

SqlFiddle here

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

2 Comments

It works, thank you. I thought c is an attribute though? Do I have them flipped or something? What am I missing? I thought b was the element and what's inside would be an attribute?
c is an attribute, b is an element. The into bit requires an element target (as obviously we can't have attributes with attributes :-)

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.