2010-07-29 92 views
2

如何檢索MS SQL中的XML字段中的字段?如何從SQL XML查詢中選擇標籤?

每當我使用此XML代碼時,我嘗試的每個查詢都無法正常工作: 我想選擇AccNumber值。

<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> 
    <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> 

我嘗試以下的多個varations沒有sucess

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

你有沒有嘗試用soap添加信封和身體:? – Fosco 2010-07-29 19:58:30

回答

4

你忽略了XML命名空間是在玩 - 你需要注意一點!

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] 

希望能夠奏效!

+0

謝謝Marc_S!這很完美!沒有需要修改的地方。 – Nic 2010-07-30 02:47:21