0
我有以下XML:如何解析XML中的兩個子節點到SQL中?
<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
任何想法,我要去哪裏錯了?
正是我在找的東西!非常感謝你。 – Chris 2012-08-16 14:53:17