Parsing XML structure in SQL Server 2008 R2 -


i using sql server 2008 r2 , stored procedure takes structured xml parameter multiple levels this.

declare @xml_account xml= <accounts>     <account>         <nameid></nameid>         <aid></aid>         <transactiontime></transactiontime>         <transactionendtime></transactionendtime>         <trasactionname></trasactionname>         <purchases>             <purchase>                 <name></name>                 <qty></qty>                 <cost></cost>                 <tax1></tax1>                 <tax2></tax2>                 <tax3></tax3>                 <tax4></tax4>             </purchase>            <purchase>                 <name></name>                 <qty></qty>                 <cost></cost>                 <tax1></tax1>                 <tax2></tax2>                 <tax3></tax3>                 <tax4></tax4>             </purchase>         </purchases>         <error></error>     </account> </accounts> 

in purchase node can repeat @ times. want store xml above in atable. tried this

select  a.b.value('account[1]/nameid[1]','varchar(10)'),         a.b.value('account[1]/aid[1]','varchar(10)'),         a.b.value('account[1]/transactiontime[1]','datetime'),         a.b.value('account[1]/transactionendtime[1]','datetime'),         a.b.value('account[1]/trasactionname[1]','varchar(10)'),         a.b.value('account[1]/purchases[1]/purchase[1]/name[1]','varchar(10)'),         a.b.value('account[1]/purchases[1]/purchase[1]/qty[1]','int'),         a.b.value('account[1]/purchases[1]/purchase[1]/cost[1]','money'),         a.b.value('account[1]/purchases[1]/purchase[1]/tax1[1]','money'),         a.b.value('account[1]/purchases[1]/purchase[1]/tax2[1]','money'),     a.b.value('account[1]/purchases[1]/purchase[1]/tax3[1]','money'),     a.b.value('account[1]/purchases[1]/purchase[1]/tax4[1]','money')     @xml_account.nodes('accounts') a(b) 

but 1 purchase node @ time. want purchase nodes.

first shred on /accounts/account 1 row each account , in cross apply shred on purchases/purchase 1 row each purchase.

select a.x.value('(nameid/text())[1]', 'varchar(10)'),        a.x.value('(aid/text())[1]', 'varchar(10)'),        a.x.value('(transactiontime/text())[1]', 'datetime'),        a.x.value('(transactionendtime/text())[1]', 'datetime'),        a.x.value('(trasactionname/text())[1]', 'varchar(10)'),        p.x.value('(name/text())[1]', 'varchar(10)'),        p.x.value('(qty/text())[1]', 'int'),        p.x.value('(cost/text())[1]', 'money'),        p.x.value('(tax1/text())[1]', 'money'),        p.x.value('(tax2/text())[1]', 'money'),        p.x.value('(tax3/text())[1]', 'money'),        p.x.value('(tax4/text())[1]', 'money') @xml_account.nodes('/accounts/account') a(x)   cross apply a.x.nodes('purchases/purchase') p(x) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -