Results 1 to 8 of 8

Thread: [RESOLVED] SQL Stored Proc Recursive Calls: Same Thread?

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Resolved [RESOLVED] SQL Stored Proc Recursive Calls: Same Thread?

    I have a stored procedure with action flags passed to perform certain operations:

    Code:
    If @nvchrAction = "DO_THIS"
    BEGIN
    
    -- do stuff
    
    END
    
    If @nvchrAction = "DO_THAT"
    BEGIN
    
    -- do stuff
    
    END

    Now, from DO_THIS I want to repeatedly call DO_THAT. It's obviously the same stored procedure, just a different copy I am invoking.

    My question is, does each call run in a separate thread? If I change a value in DO_THAT will the DO_THIS operation be affected by it?

    Thanks!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: SQL Stored Proc Recursive Calls: Same Thread?

    No, it'll keep recursing based upon the circumstances and parameters available to it from your top level call downwards. Eventually it must RETURN and continue execution in the original stored procedure and exit the whole thing.

  3. #3

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: SQL Stored Proc Recursive Calls: Same Thread?

    Thank you. So specifically:

    Any parameters I pass to the DO_THIS call (from my application) will be available to the DO_THAT call (no need to pass variables which DO_THAT needs).

    Any modifications I make to local variables in DO_THAT will be affected when I return to DO_THIS.

    Is that right?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: SQL Stored Proc Recursive Calls: Same Thread?

    Uhm... that was rather confusing.

    But after reading it several times, yes. It is just like calling any normal method in any other language.

  5. #5

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: SQL Stored Proc Recursive Calls: Same Thread?

    Thanks very much.

  6. #6

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] SQL Stored Proc Recursive Calls: Same Thread?

    After doing further research into this, I've determined that the recursive call to the stored procedure (with the DO_THAT action flag in the above example) does NOT have access to the variables set in the calling one (DO_THIS in the example). If the call to DO_THAT requires parameters, they need to be passed in explicitly, such as


    Code:
    ALTER PROCEDURE  [dbo].[TestSP]
    ...
    
     @MyVar1 int = 0
     @MyVar2 nvarchar(3) = ''
    
    ...
    
    
    
    If @nvchrAction = "DO_THIS"
    BEGIN
    
    exec dbo.TestSP  @nvchrAction = "DO_THAT", @MyVar1 = @MyVar1 , @MyVar2 = @MyVar2 
    
    END
    
    If @nvchrAction = "DO_THAT"
    BEGIN
    
    -- do stuff with MyVar1 and MyVar2
    
    END
    Otherwise they will use the default values.

    I suspect, in light of this, that the variables are passed by reference and not value.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] SQL Stored Proc Recursive Calls: Same Thread?

    My apologies, I had assumed you would be passing those parameters in. It was a "given" to me, you know what they say about assumptions.

  8. #8

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] SQL Stored Proc Recursive Calls: Same Thread?

    No worries.

    In the end I got my answer, and others may get the answer to their questions here in the future. And that's what it's all about.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width