Share via

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?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

  1. Bruce (SqlWork.com) 84,061 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.

    Was this answer helpful?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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