Tuesday, June 7, 2011

Auto Number / Sequence Generator in SQL Server

This magic code will create a sequence of numbers that can be used like a primary key without having to use a table's IDENITITY column.

SELECT ROW_NUMBER() OVER (ORDER BY col1) AS rn, * FROM tbl1