【发布时间】:2022-12-04 17:50:54
【问题描述】:
我在 sql 中有这个 xml 数据。
<Bakery xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Bakery">
<name>My Bakery</name>
<sandwiches>
<Sandwich>
<breadType>White</breadType>
<ingredients>
<Ingredient>
<name>Cucumber</name>
<price>0.05</price>
</Ingredient>
<Ingredient>
<name>Tomato</name>
<price>0.15</price>
</Ingredient>
</ingredients>
<name>Chicken Sandwich</name>
<price>0.25</price>
</Sandwich>
</sandwiches>
</Bakery>
我尝试通过以下方式查询面包店的名称:
SELECT X.Y.value('(name)[1]', 'VARCHAR(100)') as 'Bakery Name' FROM BAKERY as b
cross APPLY b.Bakery_record.nodes('Bakery') as X(Y)
但我得到的结果只是一个空单元格。
我还尝试像下面这样查询 BreadType:
SELECT X.Y.value('(breadType)[1]', 'VARCHAR(100)') as 'Bread Type' FROM BAKERY as b
cross APPLY b.Bakery_record.nodes('Bakery/sandwiches/Sandwich') as X(Y)
但结果我也得到了一个空单元格。
我错过了什么?
【问题讨论】:
标签: sql