Solved

XML Get values in ColumnA

Posted on 2011-02-17
2
923 Views
Last Modified: 2012-05-11
I am trying to get the TSQL code in SQL Server to parse the XML to show records in ColumnA
declare @XmlDoc xml, 
@iDoc integer 
select @XmlDoc = '<VendorCodesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="String" TimeStamp="2011-02-17T16:29:05" Version="2003A.TsabreXML1.0.1" SequenceNmbr="1" PrimaryLangID="en-us" AltLangID="en-us">
  <Success xmlns="http://webservices/XML/2003/07" />
  <VendorInfo xmlns="http://webservices/XML/2003/07">
    <Row RPH="1">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB />
      <ColumnC>
        <Text>LetterA</Text>
      </ColumnC>
    </Row>
    <Row RPH="2">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AB</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAB</Text>
      </ColumnC>
    </Row>
    <Row RPH="3">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AC</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAC</Text>
      </ColumnC>
    </Row>
  </VendorInfo>
  <TPA_Extensions xmlns="http://webservices/XML/2003/07">
    <HostCommand>DU*/CLS</HostCommand>
  </TPA_Extensions>
</VendorCodesRS>' 

exec sp_xml_preparedocument @iDoc output, @XmlDoc
select * from openxml
(@iDoc, 'VendorInfo',2) 
WITH 
(
ColumnA varchar(max) 'ColumnA/Text'
)

EXEC sp_xml_removedocument @idoc

Open in new window

0
Comment
Question by:garbertravel
2 Comments
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 total points
ID: 34924044
I found a way to make it work if I remove the namespace definition from the VendorInfo node (see snippet).  But unfortunately I haven't succeeded in getting it to work without any modifications to the XML.

Hopefully this will get you going though?
declare @XmlDoc xml = '<VendorCodesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="String" TimeStamp="2011-02-17T16:29:05" Version="2003A.TsabreXML1.0.1" SequenceNmbr="1" PrimaryLangID="en-us" AltLangID="en-us">
  <Success xmlns="http://webservices/XML/2003/07" />
  <VendorInfo>
    <Row RPH="1">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB />
      <ColumnC>
        <Text>LetterA</Text>
      </ColumnC>
    </Row>
    <Row RPH="2">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AB</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAB</Text>
      </ColumnC>
    </Row>
    <Row RPH="3">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AC</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAC</Text>
      </ColumnC>
    </Row>
  </VendorInfo>
  <TPA_Extensions xmlns="http://webservices/XML/2003/07">
    <HostCommand>DU*/CLS</HostCommand>
  </TPA_Extensions>
</VendorCodesRS>';

select Col.value(N'./Text[1]', N'nvarchar(100)') as ColumnA
from @XmlDoc.nodes(N'//VendorInfo/Row/ColumnA') Tab(Col);

Open in new window

0
 

Author Closing Comment

by:garbertravel
ID: 34929971
The data resides in a SQL field and I am just doing a find replace before loading the data. Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

15 Experts available now in Live!

Get 1:1 Help Now