WAITFOR Clause

When writing T-SQL Statements, the usage of WAITFOR clause will cause a delay in the execution of the subsequent statements until the condition is met.

WAITFOR clause can be used along with Time or Delay. Let us look at them with an example.

WAITFOR DELAY

When WAITFOR DELAY is used a delay duration needs to be mentioned which is of the format HH:MM:SS format followed by one thousandth of a second’s value. In the following image the Delay value mentioned is 5 seconds, hence the execution of the next statement i.e GETDATE() is delayed for 5 seconds. Find the T-SQL code used in the example below…

SELECT GETDATE()

WAITFOR DELAY '00:00:05:000'

SELECT GETDATE()


WAITFOR TIME

When WAITFOR TIME is used, a time value needs to be mentioned which is of the format HH:MM:SS format followed by one thousandth of a second’s value. In the following image the time value mentioned is 09:11:05 AM, hence the execution of the next statement i.e GETDATE() is delayed till that time occurs. Remember that the Time value is a 24 hour format of time. Find the T-SQL code used in the example below…

SELECT GETDATE()

WAITFOR TIME '09:11:05:000'

SELECT GETDATE()

Do you like this site? Like our FB page @ Facebook.com\LearnSQLWithBru so that, you know when there is a new blog post.

–Bru Medishetty

Leave a Reply