Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Numbers Table

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-05 : 16:22:55
Hi,
I am looking for best article for auxiliary numbers table on the net. I have searched but I did not find.
Please send the links to the articles.

And what do you think about this approch?
For publishing 1 to 1 million:

;WITH R_CTE_1(i) AS
(SELECT 1 UNION ALL
SELECT i + 1
FROM R_CTE_1
WHERE i < 100),R_CTE_2(i) AS
(SELECT 1 UNION ALL
SELECT i + 1
FROM R_CTE_2
WHERE i < 100),R_CTE_3(i) AS
(SELECT 1 UNION ALL
SELECT i + 1
FROM R_CTE_3
WHERE i < 100)
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 0)) AS n
FROM R_CTE_1
CROSS JOIN R_CTE_2
CROSS JOIN R_CTE_3
OPTION (MAXRECURSION 0);

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-05-05 : 17:53:23
I forget the author of this and I think I got it from SqlServerCentral, but it is supposed to be one of the fastest inline number/tally table around. But, I haven't done a lot of testing because I don't use inline stuff like this in production.
SET STATISTICS TIME ON;
DECLARE @N INT
;WITH
Tens (N) AS (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9),
Thousands(N) AS (SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3),
Millions (N) AS (SELECT t1.N FROM Thousands t1 CROSS JOIN Thousands t2),
Tally (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM Millions)

SELECT TOP (1000000)
@N = N
FROM Tally;
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-05 : 18:01:22
quote:
Originally posted by Lamprey

I forget the author of this and I think I got it from SqlServerCentral, but it is supposed to be one of the fastest inline number/tally table around. But, I haven't done a lot of testing because I don't use inline stuff like this in production.
SET STATISTICS TIME ON;
DECLARE @N INT
;WITH
Tens (N) AS (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9),
Thousands(N) AS (SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3),
Millions (N) AS (SELECT t1.N FROM Thousands t1 CROSS JOIN Thousands t2),
Tally (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM Millions)

SELECT TOP (1000000)
@N = N
FROM Tally;



Owner of this method is Itzik Ben Gan.
See:
http://www.sqlmag.com/article/sql-server/virtual-auxiliary-table-of-numbers.aspx
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-05 : 19:21:39
at last i got it.
http://www.projectdmx.com/tsql/tblnumbers.aspx
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2010-05-05 : 22:32:48
This script is for an in-line table function, F_TABLE_NUMBER_RANGE, that generates a number table. The input parameters are the @START_NUMBER and @END_NUMBER. It returns a result set containing all intergers from @START_NUMBER to @END_NUMBER inclusive.

Number Table Function
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685





CODO ERGO SUM
Go to Top of Page
   

- Advertisement -