这是另一种情况和解决方案:我正在搜索这种情况,其中使用一个查询要选择两个元素。
CREATE TABLE #Table1 (ID INT IDENTITY(1,1),XMLDoc XML)
INSERT INTO #Table1 VALUES ('
<bookstore>
<name>Bookstore1</name>
<location>Location1</location>
<book>
<title>Titile1</title>
<price>40</price>
</book>
</bookstore>')
INSERT INTO #Table1 VALUES ('
<bookstore>
<name>Bookstore2</name>
<location>Location2</location>
<book>
<title>Titile2</title>
<price>50</price>
</book>
</bookstore>')
SELECT ID,
T.c.value('title[1]','varchar(50)') AS 'BookTitile',
T.c.value('price[1]','decimal(18,2)') AS 'Price'
FROM #Table1
CROSS APPLY #Table1.XMLDoc.nodes('/bookstore/book') T(c)
DROP TABLE #Table1
您可以根据需要对其进行修改以包含 XMLNamespaces。解决方案最初位于:https ://social.msdn.microsoft.com/Forums/sqlserver/en-US/35e75e32-9ffb-4a30-8637-2cc928554763/selecting-multiple-values-from-multiple-rows-of-xml ?forum=sqlxml