[英]Get attribute and element values from xml in sql
我有以下XML,需要將這些數據提取到sql表中以獲取屬性名稱和所有元素值
declare @GetQuoteXML xml
set @GetQuoteXML = '<QuoteRequest>
<QuoteRisk>
<ChildControls parent = "MainPerson">
<OccupationID>347</OccupationID>
<OccupationDescription />
<OccupationOtherDescription>accountant</OccupationOtherDescription>
</ChildControls>
<ChildControls parent = "OtherPerson">
<OccupationID>200</OccupationID>
<OccupationDescription />
<OccupationOtherDescription>engineer</OccupationOtherDescription>
</ChildControls>
</QuoteRisk>
</QuoteRequest>'
我的SQL是
SELECT
AttributeName = ChildControls.value('(//ChildControls/@parent)[1]','varchar(50)'),
NodeName = ChildControls.value('local-name(.)', 'varchar(50)'),
NodeValue = ChildControls.value('(.)[1]', 'varchar(50)')
FROM @GetQuoteXML.nodes('//ChildControls/*') AS ChildControlTable(ChildControls)
但結果似乎總是在“ Mainperson”屬性下,並且在AttributeName列中不返回“ OtherPerson”
AttributeName NodeName NodeValue
MainPerson OccupationID 347
MainPerson OccupationDescription
MainPerson OccupationOtherDescription accountant
MainPerson OccupationID 200
MainPerson OccupationDescription
MainPerson OccupationOtherDescription engineer
我希望結果看起來像是:
AttributeName NodeName NodeValue
MainPerson OccupationID 347
MainPerson OccupationDescription
MainPerson OccupationOtherDescription accountant
OtherPerson OccupationID 200
OtherPerson OccupationDescription
OtherPerson OccupationOtherDescription engineer
我對此還比較陌生,似乎無法弄清楚,請幫忙,因為這可能很簡單!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.