Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleep Command in T-SQL?

Is there to way write a T-SQL command to just make it sleep for a period of time? I am writing a web service asynchronously and I want to be able to run some tests to see if the asynchronous pattern is really going to make it more scalable. In order to "mock" an external service that is slow, I want to be able to call a SQL server with a script that runs slowly, but isn't actually processing a ton of stuff.

like image 216
skb Avatar asked Mar 20 '09 03:03

skb


People also ask

What is sleep command in SQL?

SQL Server's Equivalent to Sleep(): The WAITFOR Statement. Posted on June 29, 2020 June 29, 2020 by Ian. In SQL Server, you can use the WAITFOR statement to delay the execution of a batch, stored procedure, or transaction. It works similar to MySQL's sleep() function.

How do I add a delay in SQL?

Using WAITFOR DELAY with a local variable. The following example shows how a local variable can be used with the WAITFOR DELAY option. This stored procedure waits for a variable period of time and then returns information to the user as the elapsed numbers of hours, minutes, and seconds.

How can I see sleep sessions in SQL Server?

Sleeping user sessions without open transactions can be identified by querying sys. dm_exec_sessions : SELECT * FROM sys.


1 Answers

Look at the WAITFOR command.

E.g.

-- wait for 1 minute WAITFOR DELAY '00:01'  -- wait for 1 second WAITFOR DELAY '00:00:01' 

This command allows you a high degree of precision but is only accurate within 10ms - 16ms on a typical machine as it relies on GetTickCount. So, for example, the call WAITFOR DELAY '00:00:00:001' is likely to result in no wait at all.

like image 131
Sam Saffron Avatar answered Sep 22 '22 06:09

Sam Saffron