Question : Finding missing identity column integers from a table

Hi experts,

Assume I have a table called orders.  The primary key and identity column is orders_id.  The column is an integer step1 and has a seed of 20000.

What I would like to do is get a list of missing rows, i.e. missing identity key values in the existing sequence.  Ideally I'd like to provide a starting point in the sequence as a parameter at run-time and have the stored proc find the max value of the orders_id column for which to search up to.

If someone could provide the code for the stored proc it would be much appreciated...

Answer : Finding missing identity column integers from a table

Your Stored Procedure would look something like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
CREATE PROCEDURE usp_FindMissingOrders
    @First_orders_id int,
    @Last_orders_id int
AS 

SET NOCOUNT ON

SELECT  n.Number Missing_orders_id
FROM    Numbers n
        LEFT JOIN Orders o ON n.Number = o.orders_id
WHERE   n.Number BETWEEN @First_orders_id AND @Last_orders_id
        AND o.orders_id IS NULL
Random Solutions  
 
programming4us programming4us