?
Solved

Reading XML into MS SQL

Posted on 2009-04-03
8
Medium Priority
?
1,646 Views
Last Modified: 2013-11-18
I'm trying to read attached xml into SQL, how to I do it.
<report_output>
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
     xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
     xmlns:rs='urn:schemas-microsoft-com:rowset'
     xmlns:z='#RowsetSchema'
     content="Report"
     title="Mod"
     resultHeading="Mod"
     resultName="Data"
     runat="01/04/09"
     user="a"
     database="ACC"
     version="5.3"
     applicationdate="02/04/09"
     >
  <s:Schema id='RowsetSchema'>
    <s:ElementType name='row' content='eltOnly'>
<s:attribute type='GeneratedPK' />      <s:attribute type='RiskName'/>
      <s:attribute type='TradeType'/>
      <s:attribute type='Value'/>
      <s:extends type='rs:rowbase'/>
    </s:ElementType>
<s:AttributeType name='GeneratedPK' rs:number='1' rs:maybenull='false' 
rs:keycolumn='true' rs:autoincrement='true' rs:writeunknown='true'>
<s:datatype dt:type='int' />
</s:AttributeType>
    <s:AttributeType name='RiskName' rs:number='2' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='200' origDataType='STRING'/>
    </s:AttributeType>
    <s:AttributeType name='TradeType' rs:number='3' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='200' origDataType='STRING'/>
    </s:AttributeType>
    <s:AttributeType name='Value' rs:number='4' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='number' dt:maxLength='50' origDataType='AMT   '/>
    </s:AttributeType>
  </s:Schema>
  <rs:data>
  <z:row
 GeneratedPK='1'
    RiskName='Interest Rate : AUD/XXX'
    TradeType='BPS/BOND'
    Value='17.605'
  />
  <z:row
 GeneratedPK='2'
    RiskName='Interest Rate : AUD/XXXX'
    TradeType='BPS/MM'
    Value='-22.0675'
  />
  <z:row
 GeneratedPK='3'
    RiskName='Interest Rate : AUD/XXXX'
    TradeType='BPS/FXSWAP'
    Value='122.483'
  />
  <z:row
 GeneratedPK='4'
    RiskName='Interest Rate : AUD/XXXX'
    TradeType='TOTAL BPS'
    Value='18.020925'
  />
  <z:row
 GeneratedPK='5'
    RiskName='Interest Rate : AUD/FRNAA'
    TradeType='TOTAL BPS'
    Value='-1.0000001'
  />
  <z:row
 GeneratedPK='6'
    RiskName='Interest Rate : AUD/C'
    TradeType='TOTAL BPS'
    Value='-199.5032'
  />
  <z:row
 GeneratedPK='7'
    RiskName='Interest Rate : USD/LIID'
    TradeType='TOTAL BPS'
    Value='-76.78061'
  />
  <z:row
 GeneratedPK='8'
    RiskName='Interest Rate : USD/C'
    TradeType='TOTAL BPS'
    Value='83.128'
  />
  </rs:data>
</xml>
 
 
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
     xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
     xmlns:rs='urn:schemas-microsoft-com:rowset'
     xmlns:z='#RowsetSchema'
     content="Report"
     title="Mod"
     resultHeading="Mod"
     resultName="Totals"
     runat="01/04/09"
     user="a"
     database="ACC"
     version="5.3"
     applicationdate="02/04/09"
     >
  <s:Schema id='RowsetSchema'>
    <s:ElementType name='row' content='eltOnly'>
<s:attribute type='GeneratedPK' />      <s:attribute type='PortfolioBPS'/>
      <s:attribute type='PortfolioMTM'/>
      <s:attribute type='ModifiedDuration'/>
      <s:extends type='rs:rowbase'/>
    </s:ElementType>
<s:AttributeType name='GeneratedPK' rs:number='1' rs:maybenull='false' 
rs:keycolumn='true' rs:autoincrement='true' rs:writeunknown='true'>
<s:datatype dt:type='int' />
</s:AttributeType>
    <s:AttributeType name='PortfolioBPS' rs:number='2' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='number' dt:maxLength='16' origDataType='AMT   '/>
    </s:AttributeType>
    <s:AttributeType name='PortfolioMTM' rs:number='3' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='number' dt:maxLength='16' origDataType='AMT   '/>
    </s:AttributeType>
    <s:AttributeType name='ModifiedDuration' rs:number='4' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='number' dt:maxLength='16' origDataType='AMT   '/>
    </s:AttributeType>
  </s:Schema>
  <rs:data>
  <z:row
 GeneratedPK='1'
    PortfolioBPS='-59.61848'
    PortfolioMTM='428917.924968'
    ModifiedDuration='0.18443'
  />
  </rs:data>
</xml>
</report_output>

Open in new window

0
Comment
Question by:emailamos
[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
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24065463
It really depends which version.  With SQL Server 2000 all you have is OPENXML().  With SQL Server 2005 there are Xml methods you can use to retrieve the data.

Unfortunately without knowing what verions you are using and what you intend to do exactly with the Xml it is difficult to help any further.
0
 

Author Comment

by:emailamos
ID: 24071347
Will be using SQL 2005 to retrieve the data.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24071976
Ok.  Can you now post what you intend to do with the data?  Structure of the tables, etc.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:emailamos
ID: 24074770
What I want to grab the values of these columns (RiskName ,TradeType , Value , PortfolioBPS,PortfolioMTM ,ModifiedDuration )
Tried below, but I don't know how to transverse the XML tree

DROP TABLE #WorkingTable
CREATE TABLE #WorkingTable
(Data XML)


INSERT INTO #WorkingTable
SELECT * FROM OPENROWSET (BULK 'C:\Temp\Output_Files\report_output.xml', SINGLE_BLOB) AS data

select * from #WorkingTable
-- to read the XML from the working table:
DECLARE @XML AS XML, @hDoc AS INT
SELECT @XML = Data FROM #WorkingTable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SELECT *
FROM OPENXML(@hDoc, '/report_output/xml/data/row', 2)
WITH (
RiskName varchar(200) '../@RiskName',
TradeType varchar(200) '../@TradeType',
Value decimal(38,4) '../@Value',
PortfolioBPS decimal(16,8) '../@PortfolioBPS',
PortfolioMTM decimal(16,8) '../@PortfolioMTM',
ModifiedDuration decimal(16,8) '../@ModifiedDuration')

EXEC sp_xml_removedocument @hDoc
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24080262
First of all Xml documents do not typically use apostrophes (single quotes) for the values, but rather quotation marks (double quotes).

But to answer your question you failed to include the Xml namespace and the XPaths are incorrect.

I am trying to post the solution, but it is getting rejected.  May have to abreviate it.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 24080270
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML, '<Root xmlns:rs=''urn:schemas-microsoft-com:rowset'' xmlns:z=''#RowsetSchema''/>'

SELECT      RiskName,
      TradeType,
      [Value],
      PortfolioBPS,
      PortfolioMTM,
      ModifiedDuration
FROM OPENXML(@hDoc, '/report_output/xml/rs:data/z:row', 2) WITH (
      RiskName varchar(200) '@RiskName',
      TradeType varchar(200) '@TradeType',
      [Value] decimal(38,4) '@Value',
      PortfolioBPS decimal(16,8) '@PortfolioBPS',
      PortfolioMTM decimal(16,8) '@PortfolioMTM',
      ModifiedDuration decimal(16,8) '@ModifiedDuration'
      )
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24080277
I had to remove the rest of the code as EE was rejecting it.  But you should get the idea.
0
 

Author Closing Comment

by:emailamos
ID: 31566147
Thanks
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

764 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