任何人都可以建议说明递归函数的编程示例吗?例如斐波那契数列或阶乘..
问问题
4108 次
3 回答
3
搜索“公用表表达式”。另请参阅此链接
更新从上面引用的链接添加示例:
;WITH Fibonacci(n, f, f1)
AS (
-- This is the anchor part
-- Initialize level to 1 and set the first two values as per definition
SELECT CAST(1 AS BIGINT),
CAST(0 AS BIGINT),
CAST(1 AS BIGINT)
UNION ALL
-- This is the recursive part
-- Calculate the next Fibonacci value using the previous two values
-- Shift column (place) for the sum in order to accomodate the previous
-- value too because next iteration need them both
SELECT n + 1,
f + f1,
f
FROM Fibonacci
-- Stop at iteration 93 because we than have reached maximum limit
-- for BIGINT in Microsoft SQL Server
WHERE n < 93
)
-- Now the easy presentation part
SELECT n,
f AS Number
FROM Fibonacci
于 2009-11-10T16:48:25.053 回答
2
以下是我使用 google.com 找到的几篇文章;)
于 2009-11-10T16:49:04.647 回答
1
对于 CTE 查询递归,请参阅此链接。 http://www.4guysfromrolla.com/webtech/071906-1.shtml
对于 TSQL 过程/函数递归,请参阅此链接http://msdn.microsoft.com/en-us/library/aa175801%28SQL.80%29.aspx
于 2009-11-10T17:08:25.730 回答