Solved

SQL Parse XML - Getting multiple records

Posted on 2011-03-22
4
311 Views
Last Modified: 2012-05-11
I have the attached data in my database xml column

When I run this code I get duplicate output...2 of each...it runs once and then again...
-- variable to store the XML content
declare @xml_text varchar(max)

 -- xml document handle             
declare @i int                                         
  
-- get XML content from XML data type field in table into a variable  
select @xml_text = cast(xml_data as varchar(max)) from xmlImportBuffer  
  
-- prepare the XML document  
exec sp_xml_preparedocument @i OUTPUT, @xml_text  
  
-- Get the PacketNum and RunDate at the <Document> node level  
-- and rpeat these fields(CROSS APPLY) for every record at the <Record> node level  
select * from  
        openxml(@i,'/Document',2)  
        with  
        (  
                PacketNum nvarchar(100),  
                RunDate nvarchar(10)  
        )  
CROSS APPLY  
(  
        select * from  
        openxml(@i,'/Document/Record',2)  
        with  
        (  
                SeqNumber int,  
                LienRefNum nvarchar(20),
                DFSNumber nvarchar(20),
                Ref1 nvarchar(1000),  
                Ref2 nvarchar(1000),
                Ref3 nvarchar(1000), 
                Ref4 nvarchar(1000),
                --RecordStatus varchar(20) './RecordStatus/@Status',  
                RecordStatus varchar(20) 'RecordStatus/@Status',    
                FilingOffice nvarchar(50)  
        )  
) s 


CROSS APPLY  
(  
        select * from  
        openxml(@i,'/Document/Record/FirstDebtorName/IndividualName',2)  
        with  
        (  
                FirstName nvarchar(50),  
                LastName nvarchar(50)  
        )  
) d 
  
-- remove the XML dcoument  
exec sp_xml_removedocument @i

Open in new window


This is the output
Screenprint
<Document>
  <XMLVersion version="1.04" />
  <PacketNum>366904_21676_20110317</PacketNum>
  <RunDate>20110317</RunDate>
  <Record>
    <SeqNumber>1</SeqNumber>
    <LienRefNum>37331766</LienRefNum>
    <DFSNumber />
    <Ref1>Our UCC Filing</Ref1>
    <Ref2>Portfolio Number</Ref2>
    <Ref3 />
    <Ref4 />
    <RecordStatus Status="InProcess" />
    <FilingType Type="Original" />
    <FileStatus Status="Submitted">03/15/2011 12:01:24 - XML filing submitted to IL</FileStatus>
    <FileNumber />
    <OriginalFileNumber />
    <FileDate />
    <OriginalFileDate />
    <ExpirationDate />
    <FilingState>IL</FilingState>
    <FilingOffice> Secretary of State</FilingOffice>
    <FirstDebtorName>
      <IndividualName>
        <FirstName>JOHN</FirstName>
        <MiddleInit />
        <LastName>BRACEWELL</LastName>
        <Suffix />
      </IndividualName>
    </FirstDebtorName>
    <RejectionReason />
  </Record>
  <Record>
    <SeqNumber>2</SeqNumber>
    <LienRefNum>37331767</LienRefNum>
    <DFSNumber />
    <Ref1>Our UCC Filing 2</Ref1>
    <Ref2>Portfolio Number 2</Ref2>
    <Ref3 />
    <Ref4 />
    <RecordStatus Status="Hold" />
    <FilingType Type="Original" />
    <FileStatus Status="Submitted">03/16/2011 12:01:24 - XML filing submitted to IL</FileStatus>
    <FileNumber />
    <OriginalFileNumber />
    <FileDate />
    <OriginalFileDate />
    <ExpirationDate />
    <FilingState>IL</FilingState>
    <FilingOffice> Secretary of State</FilingOffice>
    <FirstDebtorName>
      <IndividualName>
        <FirstName>MAX</FirstName>
        <MiddleInit />
        <LastName>SMITH</LastName>
        <Suffix />
      </IndividualName>
    </FirstDebtorName>
    <RejectionReason />
  </Record>
</Document>

Open in new window

0
Comment
Question by:lrbrister
[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
4 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35190372
use OUTER APPLY instead of CROSS APPLY
0
 
LVL 6

Accepted Solution

by:
openshac earned 500 total points
ID: 35190459
I think the problem lies with you calling the cross apply, you are essentially doing a cross product

The first select returns 1 row
The second select returns 2 rows (CROSS APPLYing returns 1x2)
The third select returns 2 rows (CROSS APPLYing returns 1x2x2 = 4 rows)

Try joining you the results of the select statements as if they were just tables
0
 
LVL 6

Expert Comment

by:openshac
ID: 35190502
Or just have this as the whole query:

select * from  
        openxml(@i,'/Document/Record',2)  
        with  
        (  
                PacketNum nvarchar(100) '../PacketNum',  
                RunDate nvarchar(10) '../RunDate',
                SeqNumber int,  
                LienRefNum nvarchar(20),
                DFSNumber nvarchar(20),
                Ref1 nvarchar(1000),  
                Ref2 nvarchar(1000),
                Ref3 nvarchar(1000), 
                Ref4 nvarchar(1000),
                --RecordStatus varchar(20) './RecordStatus/@Status',  
                RecordStatus varchar(20) 'RecordStatus/@Status',    
                FilingOffice nvarchar(50),
                FirstName nvarchar(50) './FirstDebtorName/IndividualName/FirstName',
                LastName nvarchar(50) './FirstDebtorName/IndividualName/LastName' 
        )  

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 35190506
That did it...thanks
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

705 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