CREATE TABLE Table1 (id int, name varchar(10), start int, [end] int);
GO
INSERT INTO Table1 Values
(13,'sub1', 11, 15),
(15,'sub2', 17, 22),
(19,'sub3', 44, 44);
GO
INSERT INTO Table1 Values
(13,'sub1', 11, 15),
(15,'sub2', 17, 22),
(19,'sub3', 44, 44);
;WITH CTE
AS
(
select id, name, start, [end] from Table1
union all
select id, name, start+1, [end]
from CTE where [end] > start
)
select id, name, start as 'serial' From CTE order by start option (maxrecursion 0)
id | name | serial |
---|---|---|
13 | sub1 | 11 |
13 | sub1 | 12 |
13 | sub1 | 13 |
13 | sub1 | 14 |
13 | sub1 | 15 |
15 | sub2 | 17 |
15 | sub2 | 18 |
15 | sub2 | 19 |
15 | sub2 | 20 |
15 | sub2 | 21 |
15 | sub2 | 22 |
19 | sub3 | 44 |
No comments:
Post a Comment