Recursive function in sql server 2005?

后端 未结 3 710
闹比i
闹比i 2020-12-12 01:02

Can anybody suggest programming examples that illustrate recursive functions? For example fibonacci series or factorial..

相关标签:
3条回答
  • 2020-12-12 01:14

    Here are a few articles that I found using google.com ;)

    Recursion in T–SQL
    Using recursion in stored procedures
    A Recursive User-Defined Function (SQL Server 2000)

    0 讨论(0)
  • 2020-12-12 01:30

    For CTE query recursion see this link. http://www.4guysfromrolla.com/webtech/071906-1.shtml

    For TSQL procedure/function recursion see this link http://msdn.microsoft.com/en-us/library/aa175801%28SQL.80%29.aspx

    0 讨论(0)
  • 2020-12-12 01:34

    Search for "common table expressions." See also this link

    Update Adding example from the above-referenced link:

    ;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
    
    0 讨论(0)
提交回复
热议问题