Solved

How to get OpenXML to process all nodes within parent node

Posted on 2008-10-02
2
509 Views
Last Modified: 2013-11-05
Hi there
I pass an XML string to my stored procedure with the following format:
<Root>
<Scheme ref='someref''>
<Payment>
<FundName>Fund1</FundName>
<Amount>0</Amount>
</Payment>
<Payment>
<FundName>Fund2</FundName>
<Amount>0</Amount>
</Payment>
.....
</Scheme>
<Scheme ref='anotherref'>
.....
</Scheme>
</Root>

In my stpred procedure I need to load this XML into a table with the following format:

CREATE TABLE #TempSchemes([Scheme] varchar(100) ,
                      [FundName] varchar(30) ,
                      [Amount] varchar(18))

My OpenXML statement look slike this:

EXEC sp_xml_preparedocument @nXmlHandle output, @SchemeData

INSERT INTO #TempSchemeData([Scheme], [FundName], [Amount])
SELECT xScheme, xFundName, xAmount
FROM OPENXML (@nXmlHandle, '/Root/Scheme',1)  
WITH (xScheme varchar(20) './@ref',
xFundName varchar(30) './Payment/FundName',
xAmount varchar(18) './Payment/Amount')

SELECT @nErr = @@Error, @nRowCnt = @@Rowcount
 IF(@nErr <> 0)
 BEGIN
         SELECT @nRetVal = 1  
         EXEC sp_xml_removedocument @nXmlHandle
         GOTO ERR_HANDLER
END              

THis works without error but all I am getting in my table is the FundName and Amount from my very first
<Payment> tag for each scheme when what I want is for it to load every <payment> item and give it the scheme ref of the enclosing <Scheme> tag.

Any ideas much appreciated.

Cheers
Sam
EXEC sp_xml_removedocument @nXmlHandle
        
0
Comment
Question by:Samm1502
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 22624086
Try this one:

select xScheme, xFundName, xAmount
from openxml(@idoc, 'Root/Scheme/Payment', 1)
with (
      xScheme varchar(20) '../@ref',
      xFundName varchar(30) 'FundName',
      xAmount varchar(18) 'Amount'
)
0
 

Author Closing Comment

by:Samm1502
ID: 31502330
Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question