【发布时间】:2020-02-10 18:06:30
【问题描述】:
我有两个问题:
我正在尝试在下面的示例中过滤属性
@Type = "Bikes"。但我得到了所有 6 行。我应该只得到 3 行。为什么存在不在这里工作,我没有想法?如何进一步过滤
@Type = "Bikes" and Product = "Mountain"
我的代码如下:
declare @xml xml = '<StoreSurvey>
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
<BusinessType>BM</BusinessType>
<YearOpened>1996</YearOpened>
<Specialty>Mountain</Specialty>
<SquareFeet>21000</SquareFeet>
<Brands>2</Brands>
<Internet>ISDN</Internet>
<NumberEmployees>13</NumberEmployees>
<Products Type="Bikes">
<Product>Mountain</Product>
<Product>Road</Product>
<Product>Racing</Product>
</Products>
<Products Type="Clothes">
<Product>Jerseys</Product>
<Product>Jackets</Product>
<Product>Shorts</Product>
</Products>
</StoreSurvey>'
问题 1 的代码:
select T.col.value('.','varchar(100)')
from @xml.nodes('/StoreSurvey/Products/Product') as T(col)
where T.col.exist('/StoreSurvey/Products[@Type = "Bikes"]') =1
期望的输出:
Mountain
Road
Racing
问题 2 的代码:此代码导致“语法错误”:
select T.col.value('.','varchar(100)')
from @xml.nodes('/StoreSurvey/Products/Product') as T(col)
where T.col.exist('/StoreSurvey/Products[@Type = "Bikes"]/[Product = "Mountain"]') = 1
【问题讨论】: