Sql server 在SQL Server中计算阶乘(n)?
如何计算像Sql server 在SQL Server中计算阶乘(n)?,sql-server,tsql,Sql Server,Tsql,如何计算像k这样的数学表达式在SQL Server 2012中?我真的认为在SQL Server中计算阶乘是个坏主意,但如果必须: with pair (n, f) as ( select 0 as n, 1 as f union all select p.n + 1 as n, p.f * (p.n + 1) from pair p where p.n < 5 -- factorial of 5 ) select f from pair where n = 5; -
k这样的数学表达式代码>在SQL Server 2012中?我真的认为在SQL Server中计算阶乘是个坏主意,但如果必须:
with pair (n, f) as (
select 0 as n, 1 as f
union all
select p.n + 1 as n, p.f * (p.n + 1) from pair p
where p.n < 5 -- factorial of 5
)
select f from pair where n = 5; -- factorial of 5
试试看:
创建函数阶乘(@i int)
返回整数
像
开始
声明@i int
如果@i是阶乘,你可以编写一个自定义函数来实现。请参阅示例。您尝试在数据库级别计算数学表达式是否有原因?可能的重复项没有SQL Server 2010-我们有2000、2005、2008、2008、R2、2012、2014、2016和2017-哪一个?SQL Server支持的最大整型只能容纳33个结果!最多,最简单的方法实际上是一个函数,它只会引用一个常量查找表。如果你对浮点近似法没意见,你可以更进一步。。。
f
---
120