Yes, you can substitute for a sleep or delay in SQL Server using the WAITFOR DELAY command. It pauses a query or stored procedure for a set amount of time. The syntax is simple: WAITFOR DELAY 'HH:MM:SS'.

WAITFOR DELAY: The Direct Replacement

Use WAITFOR DELAY when you want the current batch, stored procedure, or transaction to pause for a specific duration. The time is written as a string in hours, minutes, and seconds format. For example, WAITFOR DELAY '00:00:10' pauses for 10 seconds.

This is the closest direct substitute for a sleep function found in other programming languages. It works in T-SQL scripts, stored procedures, and ad-hoc queries. The delay is measured from the moment the command is executed.

One thing to note: WAITFOR DELAY does not consume CPU resources while waiting. The session is simply suspended. This makes it safe to use in loops or scheduled jobs without worrying about processor load.

WAITFOR TIME: Scheduling a Specific Moment

If you need to pause until a specific time of day, use WAITFOR TIME. The syntax is WAITFOR TIME 'HH:MM:SS'. This waits until the clock reaches that time, then continues.

For example, WAITFOR TIME '23:59:00' pauses until one minute before midnight. This is useful when you want a job to start at a precise moment, rather than after a relative delay.

Both commands can be used together in a single batch. You can wait for a time, then wait for an additional delay, then execute your next statement.

Why You Might Need a Delay in SQL Server

There are several common reasons to introduce a pause in SQL Server code:

  • Retry logic: When a transaction fails due to a deadlock or a temporary lock, you may want to wait a few seconds before trying again.
  • Staging data: If you are loading files that arrive on a schedule, a delay can give the file time to be fully written before you read it.
  • Throttling: In a loop that processes many rows, a short delay can prevent overwhelming other systems or the transaction log.
  • Testing: When simulating a slow process or a race condition, a delay helps you observe behavior under controlled timing.

In each case, WAITFOR is the tool that gives you that pause without writing complex loops or external scripts.

Retrying a Failed Transaction

Here is a practical example. Suppose you have a stored procedure that updates a table and sometimes hits a deadlock. You want to retry the update after a short pause.

  1. Set a counter to 1 and a maximum retry count of 3.
  2. Begin a loop that runs while the counter is less than or equal to the max.
  3. Try the update inside a BEGIN TRY block.
  4. If it succeeds, break out of the loop.
  5. If it fails with error 1205 (deadlock), use WAITFOR DELAY '00:00:05' to wait 5 seconds.
  6. Increment the counter and try again.

This pattern is common in production code. The delay gives the other transaction time to finish and release its locks. A 5-second pause is a reasonable starting point; you can adjust it based on how long your transactions typically run.

Alternatives to WAITFOR: When You Need Something Else

There are a few other ways to introduce a pause, but they have caveats.

Using a Loop with a Time Check

You can write a loop that checks the system time and exits after a certain number of seconds. For example, you could use GETDATE() to capture the start time, then loop while the elapsed time is less than your target. This works, but it consumes CPU and is less elegant than WAITFOR. It also can be less precise because each iteration takes a few milliseconds.

Using a Cursor or a Temporary Table

Some developers create a temporary table with a row for each second they want to wait, then use WHILE to iterate over it. This is overkill and slow. Avoid it unless you have a specific need to generate a sequence of timestamps.

Using a Scheduled Job

If you need a delay between entire jobs, consider using SQL Server Agent to schedule them with an interval. That is not a substitute for a sleep within a single script, but it can replace the need for one.

Important Considerations and Limitations

Before you use WAITFOR, keep these points in mind:

  • Transaction scope: If you are inside a transaction, WAITFOR does not release locks. The transaction remains open, and locks are held for the duration of the delay. This can cause blocking.
  • Maximum delay: The maximum delay for WAITFOR DELAY is 24 hours. For longer pauses, you would need to loop or schedule a job.
  • Precision: The delay is accurate to about 10 milliseconds on most systems, but the actual pause may be slightly longer due to scheduling.
  • Cancellation: You can cancel a query that is waiting with WAITFOR by issuing a cancel command from another session, just like any other running query.
  • Version differences: The syntax and behavior are consistent across modern versions of SQL Server, but check your specific version's documentation if you are on an older release.

If you are using WAITFOR in a loop, be careful not to create a long-running process that holds connections open. Always test with a small delay first to see the effect on your system.

Quick Reference: Common Delay Values

Desired WaitCommand
1 secondWAITFOR DELAY '00:00:01'
5 secondsWAITFOR DELAY '00:00:05'
1 minuteWAITFOR DELAY '00:01:00'
1 hourWAITFOR DELAY '01:00:00'
Until 2:30 PMWAITFOR TIME '14:30:00'

Remember that the time string uses 24-hour format. For WAITFOR TIME, the date is not specified; it always refers to today's date.

What to Do If WAITFOR Doesn't Work as Expected

If you find that WAITFOR is not pausing as you intended, check these common issues:

  • Check the time format: Ensure you are using two digits for hours, minutes, and seconds. For example, '00:00:05' is correct, not '0:0:5'.
  • Check for an active transaction: If your delay seems to cause blocking, it is likely because you are inside a transaction. Move the WAITFOR outside the transaction if possible.
  • Check for a trigger or job: Sometimes a trigger or a scheduled job may be interfering with your batch. Run your script in isolation to see if the delay works.
  • Check for a syntax error: A missing comma or a misplaced quote can cause the entire batch to fail. Review the exact syntax in your query editor.

If you still have trouble, try a simple test: run WAITFOR DELAY '00:00:02' in a new query window and see if it takes about two seconds to complete. That confirms the command is working on your server.

In short, WAITFOR is the standard, built-in way to add a sleep in SQL Server. Use it for retries, throttling, and testing. For longer pauses or scheduling, consider SQL Server Agent jobs instead.