开发者

How can I select tags from an SQL XML Query?

开发者 https://www.devze.com 2023-01-09 05:43 出处:网络
How can I retrieve the fields within an XML field in MS SQL? Every query I try does not work as intended whenever I use this XML code:

How can I retrieve the fields within an XML field in MS SQL?

Every query I try does not work as intended whenever I use this XML code: I want to select the AccNumber value.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header开发者_如何学Go>
    <AuthSoapHd xmlns="https://temp.uri.com/Mngmt/">
      <System>System1</System>
    </AuthSoapHd>
  </soap:Header>
  <soap:Body>
    <PrintList xmlns="https://temp.uri.com/Mngmt/">
      <Account>
        <AccNumber xmlns="https://temp.uri.com/Project/Object/Data">990368644</AccNumber>
      </Account>
    </PrintList>
  </soap:Body>
</soap:Envelope>

I tried multiple varations of the following with no sucess

Select [RequestXML].query('/Envelope/Body/PrintList/Account/AccNumber')
  FROM [dbo].[Table1]


You're ignoring the XML namespace that is in play - you need to pay attention to that!

WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS soap,
                   'https://temp.uri.com/Project/Object/Data' AS data,
                   'https://temp.uri.com/Mngmt/' AS mgmt)

SELECT 
   RequestXML.value('(/soap:Envelope/soap:Body/mgmt:PrintList/mgmt:Account/data:AccNumber)[1]',
                    'BIGINT') AS 'AccNumber'
FROM 
   [dbo].[Table1]

That hopefully works!

0

精彩评论

暂无评论...
验证码 换一张
取 消