Technical Article

Generate consecutive numbers

,

This script has the advantages of being fast and not having any dependencies to existing tables so it can easily be used to generate a tally table, or a set of dimension keys. It becomes especially useful when used as the left part of a join (to control cardinality) or the left part of a cross apply (to replace a while loop for instance).

DECLARE@N INT;
SET@N = 100000;

SELECT  TOP (@N) ROW_NUMBER() OVER(ORDER BY (SELECT 1)) N
FROM    master.sys.columns A CROSS JOIN master.sys.columns B CROSS JOIN master.sys.columns C

Rate

4 (6)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (6)

You rated this post out of 5. Change rating