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?
Execute stored procedure sequentially inside another stored procedure
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
7 answers
Sort by: Most helpful
-
Olaf Helper 47,436 Reputation points
2020-12-08T12:37:25.597+00:00 -
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. -
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.
-
Poornima 21 Reputation points
2020-12-08T13:01:34.333+00:00 Please look below example.
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.
-
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.