
I'm taking a SQL course and have an exam later - hoping this doesn't trip me up.
The example in the book shows this:
DECLARE @Orders XML;
SET @Orders = N'
<Orders>
<Order Product="Bike" Quantity="1" />
<Order Product="Bike" Quantity="2" />
<Order Product="Car" Quantity="4" />
</Orders>';
SELECT
tab.col.value('@Product', 'NVARCHAR(50)') AS Product
,tab.col.value('@Quantity', 'INT') AS Quantity
,tab.col.value('count(../Order)', 'INT') AS TotNumOfOrders
FROM @Orders.nodes('/Orders/Order[xs:integer(@Quantity) > 1]') AS tab(col);
The results are this:
Product Quantity TotNumOfOrders
Bike 2 3
Car 4 3
I see that the [xs:integer(@Quantity) > 1] filter causes this to return the two records with Quantities greater than one, but I would think that the TotNumOfOrders would then return '2'. So it seems like it is getting the total records before it does the filter. With a normal SQL statement and table, I believe it would filter out the records and then sum them.
The book must be correct because SQL Server returns the same result. I guess I just want to make sure that is the expected behavior. And if it is the opposite of normal SQL, you'd think the book would at least acknowledge that.
Thanks.
PS - Our resident DBA (true DBA, not just acting like a DBA) and programmer didn't know.