我已将一个 xml 文档导入 SQL Server,我现在正尝试将各个部分导入不同的表中。当我使用下面的查询时,它只返回一行hotel_facilities,我需要返回所有hotel_facilities 和hotel_ref。
DECLARE @Details xml
SET @Details = '<hotels>
<hotel>
<hotel_ref>105</hotel_ref>
<hotel_facilities>
<id>2</id>
<name>Disabled Facilities</name>
<id>4</id>
<name>24 Hour Reception</name>
<id>12</id>
<name>Restaurant</name>
</hotel_facilities>
</hotel>
</hotels>'
SELECT tab.col.value('../hotel_ref[1]','varchar(100)') AS 'hotel_ref',
tab.col.value('./id[1]','varchar(100)') AS 'HotelFacilityID',
tab.col.value('./name[1]','varchar(100)') AS 'HotelFacilityName'
FROM @Details.nodes('//hotels/hotel/hotel_facilities') AS tab(col)