簡體   English   中英

如何從SQL Server中的xml數據中獲取帶有條件的子元素和屬性值?

[英]How to get child element and attribute value with condition from xml data in sql server?

如何在SQL Server中獲取子值及其屬性值的條件? 如何獲得

<CheckoutAttribute ID="9">
<CheckoutAttributeValue><Value>26</Value></CheckoutAttributeValue> 
CheckoutAttributeID CheckoutAttributeValue
        9                    26

來自以下xml數據。

<Attributes>
<CheckoutAttribute ID="4">
<CheckoutAttributeValue><Value>18</Value></CheckoutAttributeValue>
</CheckoutAttribute>
<CheckoutAttribute ID="6">
<CheckoutAttributeValue><Value>22</Value></CheckoutAttributeValue>
<CheckoutAttributeValue><Value>23</Value></CheckoutAttributeValue>
</CheckoutAttribute>
<CheckoutAttribute ID="9">
<CheckoutAttributeValue><Value>26</Value></CheckoutAttributeValue>
<CheckoutAttributeValue><Value>27</Value></CheckoutAttributeValue>
</CheckoutAttribute>
<CheckoutAttribute ID="1">
<CheckoutAttributeValue><Value>1</Value></CheckoutAttributeValue>
</CheckoutAttribute>
</Attributes>
CREATE TABLE #tempTable ( id INT IDENTITY(1,1), xmlDataTest xml)
INSERT INTO #TempTable ( xmlDataTest)
VALUES ('<Attributes>
<CheckoutAttribute ID="4">
<CheckoutAttributeValue><Value>18</Value></CheckoutAttributeValue>
</CheckoutAttribute>
<CheckoutAttribute ID="6">
<CheckoutAttributeValue><Value>22</Value></CheckoutAttributeValue>
<CheckoutAttributeValue><Value>23</Value></CheckoutAttributeValue>
</CheckoutAttribute>
<CheckoutAttribute ID="9">
<CheckoutAttributeValue><Value>26</Value></CheckoutAttributeValue>
<CheckoutAttributeValue><Value>27</Value></CheckoutAttributeValue>
</CheckoutAttribute>
<CheckoutAttribute ID="1">
<CheckoutAttributeValue><Value>1</Value></CheckoutAttributeValue>
</CheckoutAttribute>
</Attributes>')

SELECT r.value('@ID','int') AS CheckoutAttributeID
        , r.query('data(CheckoutAttributeValue/Value)[1]') AS CheckoutAttributeValue
FROM #tempTable
CROSS APPLY xmlDataTest.nodes('/Attributes/CheckoutAttribute') AS x(r)
WHERE
r.value('@ID','int') = 9 

DROP TABLE #tempTable

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM