Solved

SQL Reading XML

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Calendar table 29 38
Replace statements HTML with HTML IF 8 54
SQL Server Agent "Access Denied" Error 3 31
SQL query 45 31
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

739 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