Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1115315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:08:19+00:00 2026-05-17T03:08:19+00:00

This is a follow-up from this question . Functions are not allowed to write

  • 0

This is a follow-up from this question.

Functions are not allowed to write to the database, but what if I wanted to update a record every time a function was called, specifically a recursive function?

Currently, I have a function that takes an ID and returns a float. I’d like to update a table using the given ID and the returned float. Ordinarily, a simple stored procedure could be used to call the function and then make the update. My function is recursive, and so the solution isn’t that simple…

I’m thinking about attempting to do this:

  • Create the recursive function so that it takes a table as a parameter
  • If the table is null, create it; else, copy the table to a new variable (because it will be readonly)
  • update the copied table in the function before making the recursive call, and pass the copy to the function
  • at the end, return the complete table (not sure how I will “know” is it complete yet)
  • call this function from a stored procedure that uses the returned table to make several updates

I’m looking for alternatives before I even try something like that. Seems that this has been done before.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:08:20+00:00Added an answer on May 17, 2026 at 3:08 am

    Any recursive implementation is T-SQL will sooner or later run into the @@NESTLEVEL cap:

    You can nest stored procedures and
    managed code references up to 32
    levels. The nesting level increases by
    one when the called stored procedure
    or managed code reference begins
    execution and decreases by one when
    the called stored procedure or managed
    code reference completes execution.
    Attempting to exceed the maximum of 32
    levels of nesting causes the whole
    calling chain to fail.

    But we know from CS101 than any recursive algorithm can be implemented as an iterative algorithm with the help of a stack. So you need a table to act as a stack (see Using Tables as Queues for some related discussion). Use a stored procedure, not a function, since in T-SQL ‘functions’ are something special (a data access path basically) and are not allowed to modify data. Whenever you think ‘function’ (as in C/C++/C# function or method), you really need a stored procedure. You cannot return ‘tables’ from procedures, so the output has to be a table into which you write the results.

    All this is just theory, because you did not provide the actual problem, just a description of the type of problem. If we’d know the real problem, we might say whether a stored procedure makes sense, whether using cursors is OK, whether using a #temp table is the way to go etc etc. And all these just to consider the plain vanilla algorithm. Things can get really hairy if you add size-of-data considerations and concurrency issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question: Best way to return status flag and message from a method in
I want to write a custom class that behaves like dict - so, I
suppose a dll contains the following functions extern C __declspec(dllexport) void f(bool x) {
What I'm trying to do is dynamically include one or more js files from
I am testing chardet in one of my scripts. I wanted to identify the
I'm building an application where every user will have a bunch of items. When
I'm aware that Cake HABTM associations are tricky at the best of times, but
I'm attempting to create a page that displays and refreshes an image from a
Ok, I’ve spent two days on this and am losing confidence in both myself
Hans Passant gave me a great answer here , so I thought of asking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.