Solved

SQL Reading XML

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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