T-SQL

Learn about the WAITFOR TIME & WAITFOR DELAY

Have you ever wanted to pause a command for a short period or wanted to run a transaction at a specific time.

 

Well I am going to quickly show you how to do them both using the WAITFOR command.

SELECT GETDATE()
WAITFOR DELAY '00:00:10';
BEGIN
SELECT GETDATE()
END;
Date time
Date time

The above code shows you how to delay two print current date and time commands by ten seconds. It can be easily modified for any time such as 43 minutes, 43 hours….

SELECT GETDATE()
WAITFOR TIME '12:22:00';

BEGIN

SELECT GETDATE()

END;
Date time
Date time

The above command will cause the transaction to wait until that time before executing the command. The time can be modified at your pleasure for whatever time you require.

The two processes do carry a processor overhead as the transaction will be running until it’s completed. Also, these commands can usually be replaced by an appropriately timed SQL Agent job which reduces the process overhead and makes the administration of it far easier.

Leave a Reply