Execution order in a stored procedure

Fredrik M 246 Reputation points
2025-04-01T10:31:54.2833333+00:00

Hi,

Need some help.

Easy example. Lets say I have two store procedures, A and B, as below.

CREATE RPOCEDURE dbo.A  
AS
BEGIN;
	DECLARE @id int=1;

          WHILE(@id <5);           
          BEGIN;
            EXEC sp_executesql dbo.B @id=@id

            SET @id=@id+1           
          END;
END;

I assume that the loop does not wait for procedure dbo.B to finish before it executes it again with the next parameter (@id=2), which means that it is paralell execution of the Procedure B?

Is there anyway I can accomplish that it executes sequential, meaning that the loop holds until procedure B finnished?

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,705 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 75,051 Reputation points
    2025-04-01T15:19:30.7766667+00:00

    Sql does not have async or parallel support. The sp_executesql will send all its result rows to the client before the next statement.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.