SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 shred xml
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

focus10
Starting Member

3 Posts

Posted - 05/24/2013 :  03:53:12  Show Profile  Reply with Quote
<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 Payment
1 201301 300
2 201301 100
2 201302 200

James K
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 05/24/2013 :  10:03:59  Show Profile  Reply with Quote
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.
Go to Top of Page

jackv
Flowing Fount of Yak Knowledge

United Kingdom
2019 Posts

Posted - 05/24/2013 :  11:43:18  Show Profile  Visit jackv's Homepage  Reply with Quote
You can use opeML or use Powershell on SQL Server - use this script as a basis - http://www.sqlserver-dba.com/2013/05/shredding-xml-with-powershell-and-sql-server.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

focus10
Starting Member

3 Posts

Posted - 05/26/2013 :  11:16:07  Show Profile  Reply with Quote
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 slow
any idea how to improve the query?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000