Learn about the WAITFOR TIME & WAITFOR DELAY — Parv The IT Geek

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;

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;

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.


Originally published at https://parvtheitgeek.com on March 12, 2015.

Advertisement

Published by Parvinder Nijjar

I blog at ParvTheITGeek.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: