Introduction

Often, an application needs a robust way to generate unique numbers. A few examples would be invoice numbers, order numbers, and donation receipt numbers. These numbering sequences often contain a non-numeric part, for example, online donation receipt numbers like 'WD-00000121'.

Background

In the 'old' days (when we used paper), there were pads of paper pre-printed with a sequence of numbers, e.g., bank account cheques. When you pull off the top-most sheet (or cheque), you are effectively claiming that sequence number--you remove it from the pad, and no one else can use it. In this flexible SQL solution, when we claim the next number in the sequence, we are essentially doing the same thing.

There are several ways to solve this in an application. I do not know all of them, but I have seen a few good ones. In this article, I will show a simple and a more flexible way.

One caveat

Although the code for the article is written for Microsoft SQL Server TSQL, this technique can be applied to most modern relational databases. For example, Oracle has a feature called 'Sequence' which is good but is not covered in this article (I have used the Oracle database product and have great respect for it as a product, but of late, all my work has been in MS SQL Server).

The simple solution

A simple solution is to insert into a table that has an IDENTITY column defined, and SQL Server will automatically assign the next number in the sequence to your new record, e.g.:

--This is partial code, do not try to execute this snippet of code it is just for
illustration purposes andisnot part of the 'flexible solution':CREATETABLE WebDonation (
DonationNumber INTNOTNULLIDENTITY(1,1),
DonationDate DATETIMENOTNULLDEFAULT(GETDATE()),
…
)

The advantage of this sequence is that it is automatic, easy to implement, and guaranteed by SQL Server to be unique. I have nothing against using IDENTITY columns per se; and although I have used them often where it made sense to, I do prefer a meaningful, human-readable, human-relevant key when possible.

The disadvantages with using an IDENTITY column here is that the numbering cannot be controlled by the administrators of the application, and it is an internal number that is not meaningful to the users.

Also, for example, if the application required that online donation receipt numbers begin at 00001 every month, an identity column would be hard to use; e.g., February 2009's donations would begin at "W-200902-00001", March 2009's would begin at "W-200903-00001".

The flexible solution

A more flexible solution to this problem would need to maintain the actual sequence outside of the table where it is being used. Some meta data for the sequence would also need to be stored somewhere. Another requirement for some applications is that they need more than one sequence or type of sequence.

First, I want to point out that this table's DDL (above) contains some business rules that I applied, such as:

Sequence numbers cannot be less than zero,

sequence numbers cannot be more than two billion, and

the final output has a maximum of 20-3+10=27 characters in length ([#], the Stored Procedure CASTs this as a VARCHAR(30)).

Your requirements may differ, and so you would need to make those adjustments to the table's DDL and to the Stored Procedure.

Explanation of the SequenceControl table

The first column, "SequenceKey", is the unique identifier for a sequence; this table can hold the sequence definitions for any number of sequences.

The column "LastSequence" holds the last sequence number that was used. It gets incremented in the Stored Procedure when it calculates and then claims the next number. To start a sequence off at 1 (and incrementing by 1), the initial value would be zero.

The column "SequenceFormat" contains a pattern for how we want this sequence to look in the end. It can contain a number of replaceable tags. It is mandatory to have the "[#]" tag because this is where the incrementing integer will be placed. Other possible tags that you could be added are date and time parts or the SQL User.

The column "ZeroPadToDigits" tells the Stored Procedure how many digits to make the incrementing number, padded on the left with zeros; e.g., an eight digit zero padded sequence would be "00000001", "00000002", "00000003" etc...

Using the example of online donation receipts, we would start our sequence like this:

For this example (every month, a sequence from 00001), it would not matter whether or not the INSERT failed because of a primary key violation; either way, the key would be in the table and we would just use it:

EXEC DBO.P_GetNextInSequence 'donations 2009 02';

Points of interest

It would be easy now to create a UI to administer the sequences; one UI could be used to maintain all the sequences in the application. The person administering them could choose at what number to begin.

The Stored Procedure can also be refactored into a database function (UDF) that simply returns the final formatted, varchar sequence number. This would be useful if you need to claim the next number from within your SQL code, for example, if you have a Stored Procedure to add the new donation instead of from [your favorite programming language] code.

Inside a UDF, one cannot make changes to the database, so this stored procedure cannot be used as a database user defined function. Apparently there are hacks for doing this anyways, but as hacks go, it is probably a bad idea for many reasons to defeat this rule in SQL Server. Thank you to drbarriesmith for asking a good question.