Execute new query when current query finalises I am in the process of inserting some data into a SQL Server (2016). Since this is a manual one-time load, I have not bothered spending a lot of time writing it the most efficient way; however some of the loads takes much longer than initially anticipated - so here is my question.

Can I somehow tell the server to execute a nother query immediately after the current one finalises? I would have done this manually by hitting F5 on the next part of the code - but since the load takes longer than expected, I would like the server to somehow execute the next part of the code automatically, when the first part finalises.

Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?

Hope this makes sense!

Thanks :-)
Assuming it is just a single statement that you are waiting to complete, you can first run the following to get the start time of the current request

SELECT start_time FROM sys.dm_exec_requests WHERE session_id = &lt;&lt;spid_of_other_connection&gt;&gt;


And then having got the start_time do some simple polling in another SSMS window

SET NOCOUNT ON;

WHILE EXISTS
(SELECT *
        FROM    sys.dm_exec_requests
        WHERE   session_id = &lt;&lt;spid_of_other_connection&gt;&gt;
                AND start_time = &lt;&lt;start_time_from_above&gt;&gt;)
BEGIN
  WAITFOR DELAY '00:00:10'
END

PRINT 'DO SOMETHING HERE'; 


The filter on start_time rather than just session_id is because the session_id can be recycled if the original connection is closed.