Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
focus10
Starting Member
3 Posts |
Posted - 2013-05-24 : 03:53:12
|
<Clients> <Prodacts> <Prodact> <bookkeeping> <Account> <ID>1</ID> <Details> <Payments> <Payment>300</Payment> <PayMonth>201301</PayMonth> </Payments> </Details> </Account> </bookkeeping> </Prodact> <Prodact> <bookkeeping> <Account> <ID>2</ID> <Details> <Payments> <Payment>100</Payment> <PayMonth>201301</PayMonth> <Payment>200</Payment> <PayMonth>201302</PayMonth> </Payments> </Details> </Account> </bookkeeping> </Prodact> </Prodacts></Clients>how can i get from the above a result like:ID PayMonth Payment1 201301 3002 201301 1002 201302 200 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-24 : 10:03:59
|
You can query it like shown below:SELECT c.value('Payment[1]','int')FROM @YourXML.nodes('//Payments') T(c); BUT, you will notice that it returns only two rows, and not 3.If you want to return all 3 rows, there is more work required. This would have been easy if SQL Server had a full implementation of the W3C specifications for XML (i.e., next sibling, previous sibling etc.)Since the SQL XML implementation is rather limited, the simplest thing would be if you are able to get the data with only one payment in each Payments section, or if each payment was wrapped in another xml node. For example, like shown below: <Payment_group> <Payment>100</Payment> <PayMonth>201301</PayMonth> </Payment_group> <Payment_group> <Payment>200</Payment> <PayMonth>201302</PayMonth> </Payment_group> Even without that, it can be done, but it is not pleasant. |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
|
focus10
Starting Member
3 Posts |
Posted - 2013-05-26 : 11:16:07
|
thank a lot i handled it by cross apply and it looks like:SELECT p.value('(PodactID)[1]', 'nvarchar(max)'),q.value('(CustomerID)[1]', 'nvarchar(max)'),r.value('(IsPaid)[1]', 'int'),r.value('(Payment)[1]', 'real'),r.value('(PaymentDate)[1]', 'nvarchar(max)')FROM XmlTable CROSS APPLY XML_DATA.nodes('Customers/Customer/Podact/PodactBills/PodactsBill') t(p) CROSS APPLY p.nodes('Payments/PaymentDetails[CustomerID]') a(q) CROSS APPLY r.nodes('PaymentsDetails[IsPaid]') b(r)the xml file is large and results come's up slowany idea how to improve the query? |
|
|
|
|
|
|
|