Quantcast
Viewing all articles
Browse latest Browse all 10

Find all missing records

Yes, this is called a common table expression and it is a new feature in SQL Server 2005. In a way very similar to derived tables but more powerful (you can use prior defined CTE in the next one, like here it is done with N1, N2, N3, etc.; CTEs have recursive capabilities).

On SQL Server 2000 you can simply create a table with numbers (a very useful tool) and use it directly instead of Nums. Here are a few methods to create table with numbers:
http://www.projectdmx.com/tsql/tblnumbers.aspx

But the other new feature in SQL Server 2005 is the EXCEPT operator. So, you would have to change the query to:


SELECT n AS missing_key_value 
FROM Nums
WHERE n BETWEEN 1 AND (SELECT MAX(keycol) FROM Foo)
  AND NOT EXISTS(SELECT *
                 FROM Foo
                 WHERE keycol = n);


Plamen Ratchev

Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>