Tuesday, June 16, 2009

Using CTE (Common Table Expression)

To create a collection of rows without querying a table or a repeating series of INSERT statements.

declare @seed int

select @seed = 8;

WITH RecurseTable(Id)
AS
(
SELECT @seed

UNION ALL

SELECT R.Id -1
FROM RecurseTable R
WHERE R.Id > 1
)

select *
from RecurseTable

No comments: