How does one cause a delay in execution for a specified number of seconds?
This doesn't do it:
WAITFOR DELAY '00:02';
What is the correct format?
How does one cause a delay in execution for a specified number of seconds?
This doesn't do it:
WAITFOR DELAY '00:02';
What is the correct format?
As mentioned in other answers, all of the following will work for the standard string-based syntax.
There is also an alternative method of passing it a
DATETIME
value. You might think I'm confusing this withWAITFOR TIME
, but it also works forWAITFOR DELAY
.Considerations for passing
DATETIME
:'1900-01-01'
).DATETIME
than to properly format aVARCHAR
.How to wait for 2 seconds:
A note on waiting for
TIME
vsDELAY
:Have you ever noticed that if you accidentally pass
WAITFOR TIME
a date that already passed, even by just a second, it will never return? Check it out:Unfortunately,
WAITFOR DELAY
will do the same thing if you pass it a negativeDATETIME
value (yes, that's a thing).However, I would still recommend using
WAITFOR DELAY
over a static time because you can always confirm your delay is positive and it will stay that way for however long it takes your code to reach theWAITFOR
statement.Try this example:
This is the whole script:
How about this?
If you have "00:02" it's interpreting that as Hours:Minutes.
The documentation for
WAITFOR()
doesn't explicitly lay out the required string format.This will wait for 2 seconds:
The format is
hh:mi:ss.mmm
.