common table expression - Sql server not in clause not working -
why not give records in sql server 2008?
;with pricedcategories ( select * product.category categoryid not in (select parent_categoryid product.category) ) select * pricedcategories
it seems query doesn't return values when there null values in subquery inside cte (if replace null in insert (1, null) let's (1, 0) query work). if want categories not other category's parents null values, can this:
declare @category table (categoryid int, parent_categoryid int) insert @category values (1, null), (2, 1), (3, 1), (4, 2) ;with pricedcategories ( select * @category y not exists (select parent_categoryid @category x x.parent_categoryid = y.categoryid) ) select * pricedcategories
it interesting see following approach works same approach described in question:
;with pricedcategories ( select * @category y y.categoryid <> all(select distinct parent_categoryid @category) ) select * pricedcategories
you change query use isnull function replace null numeric value never used categoryid, this:
;with pricedcategories ( select * @category categoryid not in (select isnull(parent_categoryid, -1) @category) ) select * pricedcategories
but null value means "nothing" changed actual value of -1 not true , shouldn't use it.
Comments
Post a Comment