Skip to content

Instantly share code, notes, and snippets.

@dmlogv
Last active June 17, 2019 08:36
Show Gist options
  • Save dmlogv/38eb66f5a2a461cb8f42e14714682daa to your computer and use it in GitHub Desktop.
Save dmlogv/38eb66f5a2a461cb8f42e14714682daa to your computer and use it in GitHub Desktop.
Generates the table of integer numbers in Transact-SQL (Microsoft SQL Server)
/*
Result values table
You could change it to permanent table like s/#integers/dbo.Integers/g
*/
DROP TABLE IF EXISTS #integers;
CREATE TABLE #integers (
i INT NOT NULL PRIMARY KEY
);
/*
Fast fill with integer number from 0 to 10^5 - 1
*/
WITH a AS (
SELECT a.n
FROM(VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) AS a(n)
)
INSERT INTO #integers (i)
SELECT
/* Kind of a binary magick */
a.n + 10 * a1.n + 100 * a2.n + 1000 * a3.n + 10000 * a4.n + 100000 * a5.n
FROM a
CROSS JOIN a AS a1
CROSS JOIN a AS a2
CROSS JOIN a AS a3
CROSS JOIN a AS a4
CROSS JOIN a AS a5
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment