попробуйте это:
DECLARE @StartNumber int
,@EndNumber int
SELECT @StartNumber=2
,@EndNumber=@StartNumber+9
;WITH AllNumbers AS
(
SELECT @StartNumber AS Number
, @StartNumber*(@StartNumber+0) AS Number1
, @StartNumber*(@StartNumber+1) AS Number2
, @StartNumber*(@StartNumber+2) AS Number3
, @StartNumber*(@StartNumber+3) AS Number4
, @StartNumber*(@StartNumber+4) AS Number5
, @StartNumber*(@StartNumber+5) AS Number6
, @StartNumber*(@StartNumber+6) AS Number7
, @StartNumber*(@StartNumber+7) AS Number8
, @StartNumber*(@StartNumber+8) AS Number9
, @StartNumber*(@StartNumber+9) AS Number10
UNION ALL
SELECT Number+1
, (Number+1)*(@StartNumber+0) AS Number1
, (Number+1)*(@StartNumber+1) AS Number2
, (Number+1)*(@StartNumber+2) AS Number3
, (Number+1)*(@StartNumber+3) AS Number4
, (Number+1)*(@StartNumber+4) AS Number5
, (Number+1)*(@StartNumber+5) AS Number6
, (Number+1)*(@StartNumber+6) AS Number7
, (Number+1)*(@StartNumber+7) AS Number8
, (Number+1)*(@StartNumber+8) AS Number9
, (Number+1)*(@StartNumber+9) AS Number10
FROM AllNumbers
WHERE Number<@EndNumber
)
SELECT * FROM AllNumbers a
ВЫВОД:
Number Number1 Number2 Number3 Number4 Number5 Number6 Number7 Number8 Number9 Number10
------- ------- ------- ------- ------- ------- ------- ------- ------- ------- --------
2 4 6 8 10 12 14 16 18 20 22
3 6 9 12 15 18 21 24 27 30 33
4 8 12 16 20 24 28 32 36 40 44
5 10 15 20 25 30 35 40 45 50 55
6 12 18 24 30 36 42 48 54 60 66
7 14 21 28 35 42 49 56 63 70 77
8 16 24 32 40 48 56 64 72 80 88
9 18 27 36 45 54 63 72 81 90 99
10 20 30 40 50 60 70 80 90 100 110
11 22 33 44 55 66 77 88 99 110 121
(10 row(s) affected)
сделать его динамическим SQL, чтобы получить правильные имена столбцов:
DECLARE @SQL varchar(5000)
,@StartNumber int
SET @StartNumber=2
SET @SQL='
DECLARE @StartNumber int
,@EndNumber int
SELECT @StartNumber='+CONVERT(varchar(3),@StartNumber)+'
,@EndNumber=@StartNumber+9
;WITH AllNumbers AS
(
SELECT @StartNumber AS [ ]
, @StartNumber*(@StartNumber+0) AS ['+CONVERT(varchar(3),@StartNumber+0)+']
, @StartNumber*(@StartNumber+1) AS ['+CONVERT(varchar(3),@StartNumber+1)+']
, @StartNumber*(@StartNumber+2) AS ['+CONVERT(varchar(3),@StartNumber+2)+']
, @StartNumber*(@StartNumber+3) AS ['+CONVERT(varchar(3),@StartNumber+3)+']
, @StartNumber*(@StartNumber+4) AS ['+CONVERT(varchar(3),@StartNumber+4)+']
, @StartNumber*(@StartNumber+5) AS ['+CONVERT(varchar(3),@StartNumber+5)+']
, @StartNumber*(@StartNumber+6) AS ['+CONVERT(varchar(3),@StartNumber+6)+']
, @StartNumber*(@StartNumber+7) AS ['+CONVERT(varchar(3),@StartNumber+7)+']
, @StartNumber*(@StartNumber+8) AS ['+CONVERT(varchar(3),@StartNumber+8)+']
, @StartNumber*(@StartNumber+9) AS ['+CONVERT(varchar(3),@StartNumber+9)+']
UNION ALL
SELECT [ ]+1
, ([ ]+1)*(@StartNumber+0) AS ['+CONVERT(varchar(3),@StartNumber+0)+']
, ([ ]+1)*(@StartNumber+1) AS ['+CONVERT(varchar(3),@StartNumber+1)+']
, ([ ]+1)*(@StartNumber+2) AS ['+CONVERT(varchar(3),@StartNumber+2)+']
, ([ ]+1)*(@StartNumber+3) AS ['+CONVERT(varchar(3),@StartNumber+3)+']
, ([ ]+1)*(@StartNumber+4) AS ['+CONVERT(varchar(3),@StartNumber+4)+']
, ([ ]+1)*(@StartNumber+5) AS ['+CONVERT(varchar(3),@StartNumber+5)+']
, ([ ]+1)*(@StartNumber+6) AS ['+CONVERT(varchar(3),@StartNumber+6)+']
, ([ ]+1)*(@StartNumber+7) AS ['+CONVERT(varchar(3),@StartNumber+7)+']
, ([ ]+1)*(@StartNumber+8) AS ['+CONVERT(varchar(3),@StartNumber+8)+']
, ([ ]+1)*(@StartNumber+9) AS ['+CONVERT(varchar(3),@StartNumber+9)+']
FROM AllNumbers
WHERE [ ]<@EndNumber
)
SELECT * FROM AllNumbers a'
exec(@SQL)
ВЫВОД:
2 3 4 5 6 7 8 9 10 11
------ ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
2 4 6 8 10 12 14 16 18 20 22
3 6 9 12 15 18 21 24 27 30 33
4 8 12 16 20 24 28 32 36 40 44
5 10 15 20 25 30 35 40 45 50 55
6 12 18 24 30 36 42 48 54 60 66
7 14 21 28 35 42 49 56 63 70 77
8 16 24 32 40 48 56 64 72 80 88
9 18 27 36 45 54 63 72 81 90 99
10 20 30 40 50 60 70 80 90 100 110
11 22 33 44 55 66 77 88 99 110 121
(10 row(s) affected)