0

I need to query xml data available in server

<root>
  <Parameter>
     <Param>APP_REG_NUMBER</Param>
     <Value>AL/T/2010/86</Value>
  </Parameter>
  <Parameter>
      <Param>SUBLINEID</Param>
      <Value>235931</Value>
  </Parameter>
</root>

This is the structure I am saving data into SQL Server, I need output as follows:

Filed1     , Filed2   , APP_REG_NUMBER,  SUBLINEID
something   something , AL/T/2010/86, 235931

please do the needful

1 Answer 1

1

You can use XQuery for this, but realize you are recreating key-value-pairs in XML, which negates the ability to use schemas or XQuery/XPATH in any reasonable manner. Consider changing the format to:

<root>
    <APP_REG_NUMBER>AL/T/2010/86</APP_REG_NUMBER>
    <SUBLINEID>235931</SUBLINEID>
</root>

I digress... the query you want is:

DECLARE @testXml xml = N'<root>
  <Parameter><Param>APP_REG_NUMBER</Param><Value>AL/T/2010/86</Value></Parameter>
  <Parameter><Param>SUBLINEID</Param><Value>235931</Value></Parameter>
</root>'

SELECT 
    @testXml.value('(//Parameter[Param/text()="APP_REG_NUMBER"]/Value)[1]', 'nvarchar(50)') as APP_REG_NUMBER,
    @testXml.value('(//Parameter[Param/text()="SUBLINEID"]/Value)[1]', 'nvarchar(50)') as SUBLINEID

You use the //Parameter syntax to find all Parameter elements and then filter them ([Param/text()="foobar"]) to only those which have a child named Value that have the inner text of SUBLINEID. From there, you navigate to the /Value child element and return the first result ((query)[1]).

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.