extracting data from XML using SQL -
i've xml following
declare @bathdata xml set @bathdata='<batch> <customers> <customer> <customerid>1</customerid> <product> <productid>10</productid> <productid>11</productid> </product> </customer> <customer> <customerid>2</customerid> <product> <productid>22</productid> <productid>23</productid> <productid>25</productid> </product> </customer> </customers> </batch>'
the result want following
cusomerid productid 1 10 1 11 2 20 2 23 2 35
and using following way sort out
select finaldata.r.value('customerid[1]','int') customerid,finaldata.r.value('product[1]','int') productid @bathdata.nodes('//batch/customers/customer') finaldata (r)
but not working quick response thanks
iterate on productids. can fetch customer 2 levels (../../customerid
):
select finaldata.r.value('(../../customerid)[1]','int') customerid , finaldata.r.value('(.)[1]','int') productid @bathdata.nodes('/batch/customers/customer/product/productid') finaldata (r)
dot .
works reference current node.
Comments
Post a Comment