0

Code:

DECLARE @dataxml XML = CONVERT(xml ,'<Parentnode><childnode><id>1</id></childnode></Parentnode>')

DECLARE @childnode  VARCHAR(50) 
SET @childnode = (SELECT DISTINCT 
                      r.value('fn:local-name(.)', 'nvarchar(50)') as t 
                  FROM @dataxml.nodes('//Parentnode/*') AS records(r))

SELECT @childnode

SELECT
   t.value('id[1]', 'int') AS id 
FROM
   @dataxml.nodes('/Parentnode/*[local-name(.)=sql:variable("@childnode")]/*') AS XD(t)

Output is returned as NULL, but it should comes with id value in xml

What's wrong in this code?

1 Answer 1

1

You just need to remove the /* characters at the end of your last line, like this:

@dataxml.nodes('/Parentnode/*[local-name(.)=sql:variable("@childnode")]') AS XD(t)

Also I had to remove the $ character at the end of the FROM keyword, but I'm guessing that could just be a typo. I tested this is SQL Server 2012.

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.