... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2009-04-17

Factorial in SQL Server 2005

Just for laughs, here is factorial calculation in SQL Server 2005:

with factorial as (
  select 0 as "n", CAST(1 AS DECIMAL(38)) as "n!"
  union all
  select "n" + 1, "n!" * ("n" + 1) from factorial
)
select top 34 * from factorial

Maximal recursive depth can be achieved using the MAXRECURSION option:

with maximal as (
  select 0 as n
  union all
  select n + 1 from maximal
)
select top 32768 * from maximal
option (maxrecursion 32767)

Blog Archive