Solved

SQL Reading XML

Posted on 2014-02-12
2
474 Views
Last Modified: 2014-02-12
The below T-SQL does not create an error but it only reads the first row of "FacNo,YearMonth, Visits"?  The XML file being read is below the T-SQL
DECLARE @x xml
SELECT @x = P
FROM OPENROWSET (BULK 'C:\EPD\DataTest.xml', SINGLE_BLOB) AS Products(P)

DECLARE @hdoc int
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

SELECT * FROM OPENXML (@hdoc, '/CMPDocument/Volume', 2)
WITH (
      FacNo varchar(max),
      YearMonth varchar(max),
      Visits varchar(max))

EXEC sp_xml_removedocument @hdoc

--------------------------------------- XML Table is below  ------------------
 <?xml version="1.0" encoding="utf-8"?>
<CMPDocument>
   <Volume>
      <FacNo>183</FacNo>
      <YearMonth>201312</YearMonth>
      <Visits>1</Visits>
      <FacNo>183</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
      <FacNo>203</FacNo>
      <YearMonth>201311</YearMonth>
      <Visits>1</Visits>
      <FacNo>203</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
    </Volume>
</CMPDocument>
0
Comment
Question by:dastaub
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39854599
The only reason why your code didn't work is because, SQL Server is expecting your XML to be in the format of

<CMPDocument>
   <Volume>
      <FacNo>183</FacNo>
      <YearMonth>201312</YearMonth>
      <Visits>1</Visits>
   </Volume>

   <Volume>
      <FacNo>183</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
   </Volume>
</CMPDocument>

as it is not the case, the sp_prepareXMLdocument and the openXML will take only the first nodes available and discard the rest....

you can try the below code

declare @X XML 
SET @X = 
'<?xml version="1.0" encoding="utf-8"?>
<CMPDocument>
   <Volume>
      <FacNo>183</FacNo>
      <YearMonth>201312</YearMonth>
      <Visits>1</Visits>
      <FacNo>183</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
      <FacNo>203</FacNo>
      <YearMonth>201311</YearMonth>
      <Visits>1</Visits>
      <FacNo>203</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
    </Volume>
</CMPDocument>
'

select @X




;WITH Visits AS
(
SELECT  ROW_NUMBER() OVER(ORDER BY (SELECT 1)) R1,T.c.query('.') as V FROM @X.nodes('CMPDocument/Volume/Visits') T(C)
),FACNO AS
(
SELECT  ROW_NUMBER() OVER(ORDER BY (SELECT 1)) R2,T.c.query('.') as F FROM @X.nodes('CMPDocument/Volume/FacNo') T(C)
),YearMonth AS
(
SELECT  ROW_NUMBER() OVER(ORDER BY (SELECT 1)) R3,T.c.query('.') as Y FROM @X.nodes('CMPDocument/Volume/YearMonth') T(C)
)
SELECT V.value('(/Visits)[1]','VARCHAR(100)') as Visits,F.value('(/FacNo)[1]','VARCHAR(100)') as FacNo,Y.value('(/YearMonth)[1]','VARCHAR(100)') as YearMonth
FROM Visits, FACNO,YearMonth
where R1=R2
AND R2 = R3

Open in new window

0
 

Author Comment

by:dastaub
ID: 39854846
The solution was to fix the malformed XML that you pointed out to me.  
<Item> </Item> was added and then it all worked.

  <CMPDocument>
    <Volume>
      <Item>
      <FacNo>183</FacNo>
      <YearMonth>201312</YearMonth>
      <Visits>341</Visits>
      </Item>
            <Item>
      <FacNo>183</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>431</Visits>
      </Item>
            <Item>
        <FacNo>203</FacNo>
        <YearMonth>201311</YearMonth>
        <Visits>144</Visits>
      </Item>
            <Item>
      <FacNo>203</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1323</Visits>
      </Item>
          </Volume>
  </CMPDocument>
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now