Search code examples
sql-servert-sqldelay

MS SQL Server - How to run a query one by one after a delay?


I would like to run a query and then 5 min break then after the next query.

How to do this with T-SQL in MS SQL Server?

EDIT: I run a heavy query in production environment which takes about 10 min. and causes some delays on the system. In 5 min delay the system is relaxing and completes some jobs which are queued. And then I run the next heavy query..


Solution

  • SELECT GETDATE() CurrentTime
    WAITFOR DELAY '00:00:05' ---- 5 Second Delay
    SELECT GETDATE() CurrentTime
    

    ref: http://blog.sqlauthority.com/2009/01/03/sql-server-time-delay-while-running-t-sql-query-waitfor-introduction/

    EDIT

    As @Panagiotis Kanavos is said: Any locks acquired by the first query will be kept until the entire batch completes.

    If this is a problem in your situation try to create a SQL Agent JOB with two steps and add a WAITFOR DELAY step between both.