What is recursion?

  • Could someone explain recursion to me?

  • This is a big topic. https://en.wikipedia.org/wiki/Recursion_%28computer_science%29

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I got through the first few paragraphs................way to much for my little brain.

    Thanx.

  • A lighter way to introduce the topic to folks for the first time, I have found, is through the The Droste Effect.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanx. That does make it clearer. So, in a sense, if I get a "maximum recursion" error then, the code reached it's maximum number of "images" within the "image"?

  • "In order to understand recursion, one must first understand recursion"

    It's mostly used when some code calls itself either directly or indirectly. For it to work, you need to find a way to stop the call or you'll enter an infinite loop.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • NineIron (1/13/2016)


    Thanx. That does make it clearer. So, in a sense, if I get a "maximum recursion" error then, the code reached it's maximum number of "images" within the "image"?

    Precisely, but the max number of pictures and the max limit of recursion in SQL Server are system-imposed limits. In the case of the picture-system it's the clarity of the image and in the case of SQL Server it's a stack where an upper bound was wise to implement to preserve system stability, but in theory recursion can go on forever.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • I'm running a procedure that I received from a vendor and I got a maximum recursion 100 error or something like that. So, this means that it "looped" 99 or 100 times then threw the error?

  • NineIron (1/13/2016)


    I'm running a procedure that I received from a vendor and I got a maximum recursion 100 error or something like that. So, this means that it "looped" 99 or 100 times then threw the error?

    Yes, you hit the Max Recusion limit on the query. This is a lot of times a circular reference in the data it is using.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Orlando Colamatteo (1/13/2016)


    NineIron (1/13/2016)


    I'm running a procedure that I received from a vendor and I got a maximum recursion 100 error or something like that. So, this means that it "looped" 99 or 100 times then threw the error?

    Yes, you hit the Max Recusion limit on the query. This is a lot of times a circular reference in the data it is using.

    Or maybe someone used a recursive CTE to count or generate rows. Either way, the procedure should be corrected.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (1/13/2016)


    Orlando Colamatteo (1/13/2016)


    NineIron (1/13/2016)


    I'm running a procedure that I received from a vendor and I got a maximum recursion 100 error or something like that. So, this means that it "looped" 99 or 100 times then threw the error?

    Yes, you hit the Max Recusion limit on the query. This is a lot of times a circular reference in the data it is using.

    Or maybe someone used a recursive CTE to count or generate rows. Either way, the procedure should be corrected.

    Could be. There are multiple ways to exhaust the limit.

    NineIron, are you at liberty to share the query showing the issue?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Sorry, no.

  • If you do a search on something along the lines of "Employees table and CTE query to show who reports to who" you will get the main go to example that probably most of us learned from concerning the concept of recursion, as it relates to set based operations here.

    The two main elements in a recursive query are the 1) seed 2) recursive portion, both are combined in the CTE with Union All operator. Try to do a little reading as this is a bit involved.

    ----------------------------------------------------

  • Will do. Thanx.

  • To me (programming wise), recursion is nothing more than a loop and has all of the same problems as a loop except there's no explicit loop making it a bit of a black box, which makes it a bit more difficult to understand and troubleshoot when compared to a normal loop.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 15 (of 21 total)

You must be logged in to reply to this topic. Login to reply