2017-08-25 142 views
1

我正在嘗試查詢XML文件以提取用戶/許可證信息。我似乎被卡住了。下面是我的數據集:SQL從XML查詢

<?xml version="1.0" encoding="UTF-16"?> 
<Users> 
    <User> 
<UserName>Elise</UserName> 
<IsConnected>1</IsConnected> 
    <Modules> 
     <Module> 
<KeyType>LOGISTICS-LTD</KeyType> 
<KeyDesc>Limited Logistics User</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP-ADDONS</KeyType> 
<KeyDesc>SAP AddOns</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP0000007050</KeyType> 
<KeyDesc>Workflow</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
    </Modules> 
    </User> 
    <User> 
<UserName>alice</UserName> 
<IsConnected>0</IsConnected> 
    <Modules> 
     <Module> 
<KeyType>BASIS0001000061</KeyType> 
<KeyDesc>Magma Credit Cards</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>LOGISTICS-LTD</KeyType> 
<KeyDesc>SAP Business One Limited Logistics User</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP-ADDONS</KeyType> 
<KeyDesc>SAP AddOns</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP0000007050</KeyType> 
<KeyDesc>Workflow</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
    </Modules> 
    </User> 
</UserS>' 

我想要實現是一個SQL SELECT返回這樣的數據:

UserName KeyType KeyDesc 
Elise LOGISTICS Limited Logistics 
Elise SAP-ADDONS SAP AddOns 
Elise SAP0000007050 Workflow 
Alice BASIS0001000061 Magma Credit Card 
Alice LOGISTICS Limited Logistics 
Alice SAP-ADDONS SAP AddOns 
Alice SAP0000007050 Workflow 

,但是這是我一直被退回:

UserName KeyType KeyDesc 
NULL LOGISTICS Limited Logistics 
NULL SAP-ADDONS SAP AddOns 
NULL SAP0000007050 Workflow 
NULL BASIS0001000061 Magma Credit Card 
NULL LOGISTICS Limited Logistics 
NULL SAP-ADDONS SAP AddOns 
NULL SAP0000007050 Workflow 

我不能找出如何從另一個節點返回UserName,任何幫助將是偉大的!以下是我的SQL:

DECLARE @xmlData XML 
set @xmlData='<?xml version="1.0"?> 
<Users> 
    <User> 
<UserName>Elise</UserName> 
<IsConnected>1</IsConnected> 
    <Modules> 
     <Module> 
<KeyType>LOGISTICS-LTD</KeyType> 
<KeyDesc>Limited Logistics User</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP-ADDONS</KeyType> 
<KeyDesc>SAP AddOns</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP0000007050</KeyType> 
<KeyDesc>Workflow</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
    </Modules> 
    </User> 
    <User> 
<UserName>alice</UserName> 
<IsConnected>0</IsConnected> 
    <Modules> 
     <Module> 
<KeyType>BASIS0001000061</KeyType> 
<KeyDesc>Magma Credit Cards</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>LOGISTICS-LTD</KeyType> 
<KeyDesc>SAP Business One Limited Logistics User</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP-ADDONS</KeyType> 
<KeyDesc>SAP AddOns</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
     <Module> 
<KeyType>SAP0000007050</KeyType> 
<KeyDesc>Workflow</KeyDesc> 
<DbType>MSS</DbType> 
<BitmaskOfLicensedModules>-255</BitmaskOfLicensedModules> 
<ReferingCount>0</ReferingCount> 
<InstallNo>0020534918</InstallNo> 
     </Module> 
    </Modules> 
    </User> 
</Users>' 


SELECT 
    ref.value('UserName[1]', 'NVARCHAR(100)') AS 'User' , 
    ref.value('KeyType[1]', 'NVARCHAR (100)') AS 'Type' , 
    ref.value('KeyDesc[1]', 'NVARCHAR (100)') AS 'Desc'  
FROM @xmlData.nodes('/Users/User/Modules/Module') 
xmlData(ref) 
+1

什麼是您當前的查詢,所以我們可以自己調試查詢?聽起來像一個XPath問題,可以通過一些導航屬性來解決。 –

+0

嘿,對不起,忘記發佈我的查詢!我現在編輯了這篇文章。 – JPC12

回答

2

您需要更改像這樣的查詢的選擇部分。

SELECT 
    ref.value('../../UserName[1]', 'NVARCHAR(100)') AS 'User' , 
    ref.value('KeyType[1]', 'NVARCHAR (100)') AS 'Type' , 
    ref.value('KeyDesc[1]', 'NVARCHAR (100)') AS 'Desc'  
    FROM @xmlData.nodes('/Users/User/Modules/Module') 
xmlData(ref) 
0

用戶是比模塊更高的嵌套級別。

所以你可以從XML獲得用戶節點。 然後交叉應用其中的模塊。

SELECT 
    [User].value('UserName[1]', 'NVARCHAR(100)') AS 'User' , 
    [Module].value('KeyType[1]', 'NVARCHAR(100)') AS 'Type' , 
    [Module].value('KeyDesc[1]', 'NVARCHAR(100)') AS 'Desc'  
FROM @xmlData.nodes('Users/User') Users([User]) 
CROSS APPLY [User].nodes('Modules/Module') Modules([Module]);