Execute stored procedure sequentially inside another stored procedure

Poornima 21 Reputation points
2020-12-08T12:22:04.823+00:00

We have a master procedure which calls many procedures inside as shown below.

exec SP1
exec SP2
exec SP1

We observe the procedure SP1 is triggered and immediately procedure SP2 also triggered. The second line does not wait for completion of execution of first line.

I tried using WAITFOR DEALY and it works in few places but not in all places as I do not know how much time I need to wait for the 1st procedure to complete its execution.

Please help me with any other option available to wait till the first procedure or sql gets complete.

Developer technologies Transact-SQL
SQL Server Other
{count} votes

7 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2020-12-08T12:37:25.597+00:00

    A SQL batch is always execute in sequence, there is no option to change the behaviour.
    Why do think it's not executed in sequence?

    0 comments No comments

  2. Poornima 21 Reputation points
    2020-12-08T12:40:49.437+00:00

    Procedures are executed in sequence but it does not wait till the execution gets completed. Looks like exec procedures are running in multiple threads in parallel.
    We don t want to start executing the next lines of code until the first line execution completes.

    0 comments No comments

  3. Olaf Helper 47,436 Reputation points
    2020-12-08T12:47:20.08+00:00

    No, a SQL batch runs in sequence, the execution waits until SP1 has finished, before the execution of SP2 starts.


  4. Poornima 21 Reputation points
    2020-12-08T13:01:34.333+00:00

    Please look below example.

    46295-image.png

    46244-image.png

    46215-image.png

    46140-image.png

    There is an error but still the procedure executes the last statement. Also i have written simple insert query but actually my code have lot more statements but i clearly observe it does not wait till execution completes.

    0 comments No comments

  5. Olaf Helper 47,436 Reputation points
    2020-12-08T15:15:16.287+00:00

    Datetime has a precision of 3 ms, INSERT into such a small table with such small values are pretty fast, faster then 3 ms; so your test proofs nothing then it's fast.


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.