Here is my CTE/SQL statement:
with cte as (
...
)
select 1, 0, q.quadrantID, 0, '', 1
from Quadrants q
where q.quadrantID not in (select cte.quadrantID from cte)
order by quadrantID ASC
The Quadrants table has 6 records.
The CTE in this scenario doesn’t return any records. My select should return the 6 records from Quadrants, but it doesn’t. Why is that?
If I run the following statement:
select 1, 0, q.quadrantID, 0, '', 1
from Quadrants q
where q.quadrantID not in (5)
order by quadrantID ASC
It will return 5 of the 6 rows, like it’s supposed to.
Are you aware that
NOT INwill return incorrect results if your CTE is returning NULL in the data.Try running this query and see the results you will get zero records
You should write your query with
NOT EXISTSread this for further info.http://decipherinfosys.wordpress.com/2007/01/21/32/
Edit: I wrote your query with
NOT EXISTSclause ,Try this