Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Recursion in SQL

Recursion in SQL

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasejavascriptsql-servercom
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dan sh
    wrote on last edited by
    #1

    I had an evil idea so I ran this script on my local SQL server database:

    CREATE PROCEDURE TESTPROC
    AS BEGIN
    SELECT 1
    EXEC TESTPROC
    END

    When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

    "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

    J Richard DeemingR S G D 6 Replies Last reply
    0
    • D dan sh

      I had an evil idea so I ran this script on my local SQL server database:

      CREATE PROCEDURE TESTPROC
      AS BEGIN
      SELECT 1
      EXEC TESTPROC
      END

      When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

      "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      If you change to a recursive CTE you can play around with OPTION (MAXRECURSION 100)

      Wrong is evil and must be defeated. - Jeff Ello

      1 Reply Last reply
      0
      • D dan sh

        I had an evil idea so I ran this script on my local SQL server database:

        CREATE PROCEDURE TESTPROC
        AS BEGIN
        SELECT 1
        EXEC TESTPROC
        END

        When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

        "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        @@NESTLEVEL (Transact-SQL) | Microsoft Docs[^]

        Each time a stored procedure calls another stored procedure or executes managed code by referencing a common language runtime (CLR) routine, type, or aggregate, the nesting level is incremented. When the maximum of 32 is exceeded, the transaction is terminated.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • D dan sh

          I had an evil idea so I ran this script on my local SQL server database:

          CREATE PROCEDURE TESTPROC
          AS BEGIN
          SELECT 1
          EXEC TESTPROC
          END

          When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

          "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #4

          I will take over the world before you do. Just letting you know! ;P

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          D 1 Reply Last reply
          0
          • S Super Lloyd

            I will take over the world before you do. Just letting you know! ;P

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            No it won't, unless it takes over 32 people at a time. :laugh:

            "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

            S 1 Reply Last reply
            0
            • D dan sh

              I had an evil idea so I ran this script on my local SQL server database:

              CREATE PROCEDURE TESTPROC
              AS BEGIN
              SELECT 1
              EXEC TESTPROC
              END

              When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

              "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #6

              I saw something similar happen with a trigger making a change that was then running the same trigger again. The updates were extremely slow and I discovered that there was, like you have discovered, a point at which SQL Server gave up - when the recursion was 32 deep. You would hope that the SQL Server would just catch this sort of thing at compile time.

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              1 Reply Last reply
              0
              • D dan sh

                No it won't, unless it takes over 32 people at a time. :laugh:

                "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                S Offline
                S Offline
                Super Lloyd
                wrote on last edited by
                #7

                It will take over thousands upon thousands! And it's only 10 years away around the corner!!!! :omg:

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                1 Reply Last reply
                0
                • D dan sh

                  I had an evil idea so I ran this script on my local SQL server database:

                  CREATE PROCEDURE TESTPROC
                  AS BEGIN
                  SELECT 1
                  EXEC TESTPROC
                  END

                  When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

                  "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                  D Offline
                  D Offline
                  DaveAuld
                  wrote on last edited by
                  #8

                  I resorted to a GOTO in an SQL query Loop that created a new transaction to work round a repetitive action with an unknown data set size. It was dirty, but worked. Glad I didn't try recursion as that would have definitely ended in tears by the sound of things! You can read about the problem here.....[Developing Automated Data Purge Solution](https://www.codeproject.com/Articles/689916/Developing-Automated-Data-Purge-Solution), but it basically looks like this;

                  LoopStart:
                  IF (@Count - @RowCountTotal) > @BatchSize
                  Begin
                  Begin Transaction
                  Delete Top (@BatchSize) From Comment Where CommentTime < @StartDate
                  Set @RowCountTotal = @RowCountTotal + @@RowCount
                  Commit Transaction
                  Goto LoopStart
                  End
                  Else
                  If (@Count - @RowCountTotal) > 0
                  Begin
                  Begin Transaction
                  Delete Top (@Count - @RowCountTotal) From Comment Where CommentTime < @StartDate
                  Set @RowCountTotal = @RowCountTotal + @@RowCount
                  Commit Transaction
                  End

                  Dave Find Me On:Web|Youtube|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject

                  1 Reply Last reply
                  0
                  • D dan sh

                    I had an evil idea so I ran this script on my local SQL server database:

                    CREATE PROCEDURE TESTPROC
                    AS BEGIN
                    SELECT 1
                    EXEC TESTPROC
                    END

                    When I executed the stored procedure, it stopped after 32 levels of nesting. My evil plan failed and I learnt something new today.

                    "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                    D Offline
                    D Offline
                    David A Gray
                    wrote on last edited by
                    #9

                    IMO, recursion is inherently dangerous, and must be avoided whenever possible. Moreover, when you must use recursion, you must also take steps to ensure that your recursive routine converges and unwinds its recursive stack. Every recursive routine must incorporate a test that stops the recursion in such a way that the recursion depth is self-limiting. This is true regardless of the programming language in which the recursion occurs. Whenever I encounter a recursion, my first thought is to investigate other algorithms that don't rely on recursion. More often than not, I have managed to succeed in finding one.

                    David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups