2

任何人都可以建议说明递归函数的编程示例吗?例如斐波那契数列或阶乘..

4

3 回答 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 找到的几篇文章;)

T–SQL中的
递归 在存储过程中
使用递归 用户定义的递归函数 (SQL Server 2000)

于 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 回答