2

I am trying to pass a local variable within a XML.modify function as a node within my SQL Script as the xml trying to be modified is dynamic. See code below

DECLARE
    @XML VARCHAR(MAX) = 'ns:Messages/ns:T001.0_NewSPIDRequests/ns:T001.0_NewSPIDRequest/ns:D2009_SWConnectionRef'

;WITH XMLNAMESPACES('urn:bridgeall-com:cmaservice:data:v3' AS ns)
UPDATE XMLMessageFeeder
SET theXML.modify('replace value of (/ns:Submission/*[local-name(.) = sql:variable("@XML")]/text())[1] with ("New Value")')

SELECT theXML 
FROM XMLMessageFeeder

The issue I'm having is the above doesn't update the the value of that node yet if I was to set the replace as the following:

SET theXML.modify
             ('
                   replace value of (/ns:Submission/ns:Messages/ns:T001.0_NewSPIDRequests/ns:T001.0_NewSPIDRequest/ns:D2009_SWConnectionRef/text())[1]
                  with ("New Value")
             ')

This works. From my knowledge (which is little) they are both passing the same replace code yet using a variable doesnt seem to work.

Can someone point me in the right direction?

EDIT Answer can be found below in the comments.

1 Answer 1

1

local-name(.) returns the name of the current node. You can not use local-name(.) to compare against a path expression. Only one node at a time.

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

4 Comments

so having all nodes within one variable is basically a dead end? I would have to have multiple variables which contain each individual node that im looking for. EG Variable1 = T001.0_NewSPIDRequests, Variable2 = T001.0_NewSPIDRequest etc, then within the replace value have it something along the lines of (/ns:Submission/ns:Messages/ns:*[local-name() = sql:variable("@Variable1")]/ns:*[local-name() = sql:variable("@Variable2")]
@Blahwoo Yes. You can do it with multiple variables but you have to decide the depth of the path beforehand (looks like four in your case). You could of course build the update statement dynamically and execute using sp_executesql. Or if the final leaf node name is enough to find the node you are looking for you can use // to do a deep search for a node. Something like //*[local-name(.) = sql:.....
Thanks for that! That has help me out alot :)
@Blahwoo, I just pushed you over the magic 15 rep points so you might vote up here too :-)

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.