Another of those #sqlhelp inspired posts. This was around ways to track down missing numbers in a sequence table. This task can be done in multiple ways and as I like to say that there are multiple ways to skin a cat! However, this blog post is about using the new T-SQL enhancements in SQL Server 2012 to figure out missing sequential numbers. I will demonstrate this using an example.
I have a table tblsequences which has two integer columns with the primary key being an int datatype. I did some random inserts into the table. The script below can be used to find out missing sequential numbers in an identity column or an integer column which is supposed to store sequential values.
-- Replace starting value with minimum starting value and increment for your sequence -- Replace the table name with the table name that you are interested in declare @startvalue int = 1, @increment int = 1 ;with cte as ( select a,(a-lag(a,1) OVER (ORDER BY a)) as MissingSequences from tblsequences ) select a, (MissingSequences/@increment)-1 as MissingSequences from cte where MissingSequences > @increment union all select TOP 1 MIN (a), CASE (MIN(a)- @startvalue)/@increment when 0 then null else (MIN(a)- @startvalue)/@increment end as MissingSequences from tblsequences group by a order by a
The output is as shown below in the screenshot. You will notice that the first column reports the primary key value and the MissingSequences column reports the number of missing sequential values lesser than the value in the first column. You will need to make the necessary changes to the two scripts shown in this blog post so that the tables you want analyzed are done so.
Next, lets talk about Sequences which is a new feature in SQL Server 2012. I created a new sequence using the a start value of 10 and an increment value of 3.
I used the Sequence which was created for generating OrderID values in two different tables. Now we are posed with an interesting problem. I have two different tables in which I want to compare missing sequence numbers. This can be done with T-SQL code below.
-- Replace schema name, table name(s) and sequence name as appropriate declare @startvalue int = 1,@interval int = 1, @seqname sysname = 'TestSeq', @schemaname sysname = 'dbo' select @startvalue = TRY_CAST(TRY_CAST(start_value as varchar(255)) as int), @interval = TRY_CAST(TRY_CAST(increment as varchar(255)) as int) from sys.sequences where name = @seqname and [schema_id] = (select [schema_id] from sys.schemas where name = @schemaname) if (@startvalue IS NOT NULL and @interval IS NOT NULL) begin ;with cte as ( select OrderID,(OrderID-lag(OrderID,1) over (order by OrderID)) as MissingSequences from (select OrderId as OrderID from tblTestSeq union all select OrderId as OrderID from tblTestSeq_2) A ) select OrderID, (MissingSequences/@interval)-1 as MissingSequences from cte where MissingSequences > @interval union all select TOP 1 OrderID, CASE (MIN(OrderID)- @startvalue)/@interval when 0 then null else (MIN(OrderID)- @startvalue)/@interval end as MissingSequences from tblTestSeq group by OrderID order by OrderID end else else begin PRINT 'CAST FAILED' end
The logic used is the same as the logic when we were looking for missing sequences in identity/integer columns described in the first half of the post. However, since sequences can be used for one or more tables, I have used a UNION query to get all the columns together which use the same sequence object. The second result set of the output in the screenshot below shows the tables involved along with the sequential OrderIDs. As you can see below, there are 4sequence numbers missing 10, 13, 16 and 28. The first result set reports exactly that by notifying that there are 3 sequence ids missing lesser than 19 and 1 sequence number missing lesser than 31.
The output of the above T-SQL script is shown in the screenshot below.
Another way how T-SQL enhancements in SQL Server 2012 can make your life easier.
Happy T-SQL coding and a have a great weekend!