我有以下 XML:
<xmlRoot>
<DomainName>sample.com</DomainName>
<UserCountsByTemplate>
<KeyValueOfstringint>
<Key>default</d2p1:Key>
<Value>20</d2p1:Value>
</KeyValueOfstringint>
<KeyValueOfstringint>
<Key>basic</d2p1:Key>
<Value>66</d2p1:Value>
</KeyValueOfstringint>
</UserCountsByTemplate>
</xmlRoot>
我试图让两行像这样返回:
Domain TemplateName TemplateCount
---------- ------------ -------------
sample.com default 20
sample.com basic 66
我尝试了以下方法:
DECLARE @info XML
SELECT @info = '<xmlRoot><DomainName>sample.com</DomainName><UserCountsByTemplate><KeyValueOfstringint><Key>default</Key><Value>20</Value></KeyValueOfstringint><KeyValueOfstringint><Key>basic</Key><Value>66</Value></KeyValueOfstringint></UserCountsByTemplate></xmlRoot>'
SELECT
row.value('DomainName[1]','NVARCHAR(255)') AS [DomainName]
, row2.value('.','NVARCHAR(255)') AS [TemplateName]
, row3.value('.','INT') AS [TemplateCount]
FROM @info.nodes('/xmlRoot[1]') T1(row)
CROSS APPLY @info.nodes('/xmlRoot/UserCountsByTemplate/KeyValueOfstringint/Key') T2(row2)
CROSS APPLY @info.nodes('/xmlRoot/UserCountsByTemplate/KeyValueOfstringint/Value') T3(row3)
但是得到以下结果:
Domain TemplateName TemplateCount
---------- ------------ -------------
sample.com default 20
sample.com basic 20
sample.com default 66
sample.com basic 66
关于我在哪里出错的任何想法?