我试图查询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 )
答案 0 :(得分: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 )
答案 1 :(得分: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]);