Uncategorized

Tally Table

Create Tally table and generate 100 million numbers. Change the CROSS JOIN part to limit the number of values generated.

CREATE TABLE [Tally] ( [N] [INT] NOT NULL PRIMARY KEY )
GO

;WITH cte(N) AS
(
	SELECT 
		ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
	FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a (n)
			CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b (n)
			CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c (n)
			CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n)
			CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n)
			CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n)
			CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n)
)
INSERT INTO [Tally] (N)
SELECT N
FROM cte
ORDER BY 1
GO