tsql - Xquery to find the value of an xml element -
all,i trying value of element value when value of name org_id.this xml placed in database external process don't have control over. there multiple parematervalue nodes ,so not sure position of value element when name org_id.the guarantee there 1 element name value org_id.
is there way find value of value in scenario?
sample xml(my_xml)
<parametervalues> <parametervalue> <name>car_model</name> <value>all</value> </parametervalue> <parametervalue> <name>debug</name> <value>0</value> </parametervalue> <parametervalue> <name>org_id</name> <value>123456</value> </parametervalue> </parametervalues> xquery select my_xml.value('(/parametervalues/parametervalue/value)[3]','int') org_wk #test_xml my_xml.exist('/parametervalues/parametervalue/name[text() = "org_id"]') = 1
this should trick:
-- sample data create table #test_xml (my_xml xml); insert #test_xml values ('<parametervalues> <parametervalue> <name>car_model</name> <value>all</value> </parametervalue> <parametervalue> <name>debug</name> <value>0</value> </parametervalue> <parametervalue> <name>org_id</name> <value>123456</value> </parametervalue> </parametervalues>'); -- solution select x2.p.value('(text())[1]', 'varchar(100)') #test_xml x1 cross apply x1.my_xml.nodes('//parametervalue[name="org_id"]/value') x2(p);
Comments
Post a Comment