Let suppose my table can have values from 000 to 999 (three digits and less than 1000)
Some of this values are filled. Let’s suppose currently my table has
000,002,005,190 (001,004,003,006,..189,191,..,999 can be inserted into table)
and these values are randomly allocated 000 and 002 is in table but 001 is not in table yet.
How can I get the values that I can insert into table yet.
EDIT
This query works by generating the complete list of possible numbers from a system table (
master.dbo.spt_values) which is guaranteed to contain more than 1000 rows inside the CTErnCTE. -1 is added toROW_NUMBERto have the values start at 0 rather than 1.The outer query zero pads the numbers for display, returning only those which are not in the source data and are less than 1000.