-
Notifications
You must be signed in to change notification settings - Fork 681
/
udf_Tally.sql
52 lines (49 loc) · 1.15 KB
/
udf_Tally.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
CREATE FUNCTION [dbo].[udf_Tally]
(
@pMin BIGINT
,@pMax BIGINT
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
/* Uses the Bates algorithm for generating the rows from */
/* http://www.sqlservercentral.com/scripts/Tally+Table/99617/ */
/*
Usage:
SELECT N FROM [dbo].[itvfTally](1001,1100)
SELECT N FROM [dbo].[itvfTally](-10,100)
SELECT N FROM [dbo].[itvfTally](0,1000)
SELECT N FROM [dbo].[itvfTally](1,1000)
*/
WITH T1(F) AS
(
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1
),
T2(F) AS
(SELECT 1 FROM T1 A, T1 B),
T3(F) AS
(SELECT 1 FROM T2 A, T2 B),
T4(F) AS
(SELECT 1 FROM T3 A, T3 B),
cteTally(N) AS
(
SELECT TOP ((@pMax-(((ABS(@pMin)+@pMin)/2)))+1)
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
+ ((((ABS(@pMin)+@pMin)/2))-1)
FROM T4
)
SELECT
N
FROM
cteTally T
;