Factorial in SQL
Example for versions
Microsoft SQL Server 2005
This example uses recursive factorial definition, implemented as a recursive query — a feature included in SQL Server 2005 and higher. Each row of recursive query contains two numeric fields — n and f = n!, and each row is calculated using the data of the previous row.
with factorial(n, f) as
(
select 0, 1
union all
select n+1, f*(n+1) from factorial where n<16
)
select cast(n as varchar)+'! = '+cast(f as varchar)
from factorial
Comments
]]>blog comments powered by Disqus
]]>