Solved

SQL Reading XML

Posted on 2014-02-12
2
485 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
[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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

734 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