I am trying to find out the cities where there is a supplier with the name of Herman. We are using XPath through Microsoft SQL Server Management Studio.
I am not a CIS major and I have no idea where to start. So that's why I'm asking for help...
I have the following table created:
CREATE TABLE Tb_Supplier(
XMLColumn XML)
GO
INSERT Tb_Supplier VALUES(
'<SuppliersList>
<Supplier name="Joe">
<City>Paris</City>
<Product name="Airplane"/>
<Product name="Milk"/>
<Product name="TV"/>
<Product name="Orange"/>
</Supplier>
<Supplier name="Herman">
<City>Chicago</City>
<Product name="Orange"/>
</Supplier>
<Supplier name="Bernstein">
<City>Madison</City>
<Product name="Truck"/>
<Product name="TV"/>
</Supplier>
<Supplier name="Hunter">
<City>Wausau</City>
</Supplier>
<Supplier name="Mayer">
<City>Madison</City>
</Supplier>
<Supplier name="Rosenfeld">
<City>Chicago</City>
<Product name="Computer"/>
<Product name="Book"/>
<Product name="Truck"/>
</Supplier>
</SuppliersList>');
The output is supposed to show:
<City>Chicago</City>
I got the it to work by the following:
SELECT XMLColumn.query('/SuppliersList/Supplier[@name="Herman"]/City')
FROM Tb_Suppliers
I am kind of getting the grasp of things.
Currently I am having trouble how to find out the following:
List the products are TV's, and are offered in Madison.
Obviously the output will be since it is looking for TV.
I have this so far, I don't know what I'm doing wrong as this makes the most sense to me.
SELECT XMLColumn.query('/SuppliersList/Supplier/Product[@name="TV"]/../City[@name="Madison"]/../Product')
FROM Tb_Suppliers
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…