1

I have the below XML:

create table #temp(cid int,xml_data xml)
insert into cid
values(1001,
     '<Main>
        <name>''John doe''</name>
        <age>15</age>
    </Main>')

I want to add an additional node to this XML based on a simple parametric condition:

desired output:

<Main>
    <name>John doe</name>
    <type>Q</type>
    <age>15</age>
</Main>

code:

select case when @type = 'Q' then
    UPDATE #temp
    SET Main.modify('insert <type = 'Q'> into 
        (/Main)')
    GO

I am getting syntax error. Any help!

UPDATE:

I implemented the suggested solution in my code and I'm getting below error. Missing out something silly!

 UPDATE #temp
         SET xml_data = 
            case
                when @type = 'G' 
                then xml_data.modify('insert <type>G</type> into (/Main)[1]');
                when @type = 'Q' 
                then xml_data.modify('insert <type>Q</type> into (/Main)[1]'); end

I am getting 'Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.' error

1
  • 2
    The syntax error is pretty obvious: Your insert <type = 'Q'> trys to insert a not well-formed node... Btw: Do you really want to wrap "John doe" in quotes? Commented Jun 20, 2018 at 8:07

2 Answers 2

4

No need for any complicated hassel. Just insert the node you want as you want it:

UPDATE #temp SET xml_data.modify('insert <type>Q</type> into (/Main)[1]');

Using as first, as last or before / after allows you to specify the node's position. The following will place the new node directly after <name>:

UPDATE #temp SET xml_data.modify('insert <type>Q</type> after (/Main/name)[1]');

UPDATE Your question about an update-statement

Your statement has several flaws:

UPDATE #temp
     SET xml_data = 
        case
            when @type = 'G' 
            then xml_data.modify('insert <type>G</type> into (/Main)[1]');
            when @type = 'Q' 
            then xml_data.modify('insert <type>Q</type> into (/Main)[1]'); 
         end

You cannot use the syntax SET xmlColumn = xmlColumn.modify(). You have to use SET xmlColumn.modify(), Furthermore the semicolons are breaking this anyway.

To be honest, I think this is to complicated, try this:

DECLARE @type VARCHAR(1)='Q'
UPDATE #temp SET xml_data.modify('insert <type>{sql:variable("@type")}</type> into (/Main)[1]');

This will create a new node <type>content</type>, with a content taken out ot the variable @type.

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

1 Comment

this approach is looking simple. But I am missing out something silly that I'm getting an error. Updating the question with my new error and code trial
2

I was able to achieve this by breaking the update into two statements:

create table #temp(cid int,xml_data xml)
insert into #temp
values(1001,
     '<Main>
        <name>''John doe''</name>
        <age>15</age>
    </Main>')


    UPDATE #temp
    SET xml_data.modify('insert <type /> into (/Main)[1]')
    GO

    declare @variable xml = 'q'
    UPDATE #temp
    set xml_data.modify('insert sql:variable("@variable") as last into (/Main/type)[1]')
    GO

    select * from #temp

enter image description here

1 Comment

No need for this approach. You can insert the new node in one go easily. See my answer...

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.