How to get values from xml in SQL Server 2012 -
i have table 9000 rows , need every row need values , name values xml.
table columns: eaid
, created
, attachedapplication
xml:
<namedapplication xmlns:xs="http://www.w3.org/2001/xmlschema-instance"> <mainpart> <firstname>john</firstname> <surname>smith</surname> <extras /> </mainpart> </namedapplication>
i can't results in table. name , surname resulted null
try this:
select eaid, firstname = xc.value('(firstname)[1]', 'varchar(100)'), surname = xc.value('(surname)[1]', 'varchar(100)') dbo.yourtablename cross apply attachedapplication.nodes('/namedapplication/mainpart') xt(xc)
this "enumerate" on table, returning eaid
column, , "reach into" xml , grab <firstname>
, <surname>
nodes within xml , expose them separate columns in output.
update: if run example uses table variable - work , return values??
declare @table table (eaid int, created date, attachedapplication xml) insert @table (eaid, created, attachedapplication) values (1, getdate(), '<namedapplication xmlns:xs="http://www.w3.org/2001/xmlschema-instance"> <mainpart> <firstname>john</firstname> <surname>smith</surname> <extras /> </mainpart> </namedapplication>') select eaid, firstname = xc.value('(firstname)[1]', 'varchar(100)'), surname = xc.value('(surname)[1]', 'varchar(100)') @table cross apply attachedapplication.nodes('/namedapplication/mainpart') xt(xc)
Comments
Post a Comment