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
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]
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.