Question: How to Introduce Time Delay Between Two TSQL Commands?
Answer: Every time when I hear this question, I am very confident this question is from a developer. This is because I have observed quite a lot of time in developer world this question being discussed. There are processes and functions which need to have a time delay between them and the programming language developer knows how to use various delays related time functions to achieve it.
However, it is really not something I see in the database world. Trust me personally I have yet not come across a situation where I would need delay between two commands in the database. There are different ways to achieve the same output with a different strategy of code in SQL Server. Anyway, in the interview processes sometimes as a candidate we are more incline to answer interviewers than argue with them.
Let us see how we can add delay between two T-SQL statement in following example.
In following example, I am adding a 10 second delay between two SELECT statement.
SELECT GETDATE() CurrentTime WAITFOR DELAY '00:00:10' -- 10 Second Delay SELECT GETDATE() CurrentTime
When you execute above script, it gives us following output.
You can clearly see in the result set that there are 10 second delay between two SELECT statement.
Now here is a question back to you? Have you ever used WAITFOR DELAY function in your business in SQL Server? If yes, would you please leave a comment so we can learn from you.
Here are two blog posts and a video post on the same subject I have written earlier.
- Time Delay While Running T-SQL Query – WAITFOR Introduction
- Delay Function – WAITFOR clause – Delay Execution of Commands
- Video Delay Command in SQL Server – SQL in Sixty Seconds #055
Reference: Pinal Dave (https://blog.sqlauthority.com)