Why this does not give any records in sql server 2008?
;with pricedCategories as
(
select * from Product.Category where CategoryID not in
(select Parent_CategoryID from Product.Category)
)
select * from pricedCategories
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It seems that your query doesn’t return values when there are NULL values in subquery inside CTE (if I replace NULL in insert (1, NULL) with let’s say (1, 0) your query will work). If you want to get the categories that are not any other category’s parents even with NULL values, you can do it like this:
It is interesting to see that the following approach works the same as the approach described in your question:
You could change your query to use the ISNULL function to replace NULL with some numeric value that is never used as CategoryID, like this:
But then the NULL value which means “nothing” would be changed to actual value of -1 which is not true and you shouldn’t use it.