How to create an auto-incrementing column in MS SQL Server 2000

Unlike Microsoft SQL Server 2005 and 2008, MS SQL Server 2000 does not have a ROW_NUMBER() function which applies only to the results of a SELECT query as it doesn’t store any permanent value in the DB.

The way it works is in keeping with the principles of relational databases. There is no implict ordering of records in an RDBMS. Only by using ORDER BY, a particular ordering (and not sorting) can be enforced only when doing a SELECT.

Since Microsoft SQL Server 2000 doesn’t have ROW_NUMBER(), the need for creating a primary key column of format int with the auto-increment functionality can be achieved using the keyword IDENTITY(1,1). IDENTITY means “a unique IDENTITY for the record so long as this table exists”.

Usage example:

CREATE TABLE jobs
(
job_id smallint IDENTITY(1,1)PRIMARY KEY CLUSTERED,
job_desc varchar(50) NOT NULL DEFAULT
min_lvl tinyint NOT NULL CHECK (min_lvl >= 10),
max_lvl tinyint NOT NULL CHECK (max_lvl <= 250) ) 

Microsoft’s T-SQL reference under SQL Server 2000 documentation at MSDN explains the IDENTITY property in detail at http://msdn.microsoft.com/en-us/library/aa933196(SQL.80).aspx