4
votes

I google a lot and got no luck. I can't retrieve data from XML column which data came from web service using sp_OAGetProperty.

the XML Column contain..

<ArrayOfCustomerInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
   <Customer CustCode="001">
      <CustName>John</CustName>
      <Queues>
         <Q>
            <No>10</No>
            <Line>1</Line>
         </Q>
      </Queues>
   </Customer> 
</ArrayOfCustomerInfo>

I got NULL when I execute following statement

(but works fine if I remove all XML namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/")

SELECT a.b.value('@CustCode','varchar(4)') AS Code
   ,a.b.value('CustName[1]','varchar(20)') AS Name
   ,c.d.value('No[1]','int') AS QNo
   ,c.d.value('(Line)[1]','int') AS QLine
FROM  PGHRMS_Employees x
CROSS APPLY x.data.nodes('/ArrayOfCustomerInfo/Customer') AS a(b)
CROSS APPLY a.b.nodes('Queues/Q') AS c(d)

please give me some advice. I've to achieve with SQL SERVER :(

If anyone want to reproduce it, I pasted script at : http://pastebin.com/ueZGidyL

Thank you in advance !!!

1

1 Answers

3
votes

Try this:

;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/')
SELECT
    Code = XC1.value('@CustCode', 'varchar(4)'),
    Name = XC1.value('CustName[1]', 'varchar(20)'),
    QNo = XC2.value('No[1]', 'int') ,
    QLine = XC2.value('(Line)[1]','int') 
FROM
    PGHRMS_Employees 
CROSS APPLY
    XmlContent.nodes('/ArrayOfCustomerInfo/Customer') AS XT1(XC1)
CROSS APPLY 
    XC1.nodes('Queues/Q') AS XT2(XC2)

With the WITH XMLNAMESPACES construct, you can define some XML namespaces to be used by the following T-SQL statement - default or prefixed namespaces alike.