How to Introduce Time Delay Between Two T-SQL Commands? – Interview Question of the Week #091

Question: How to Introduce Time Delay Between Two TSQL Commands?

How to Introduce Time Delay Between Two T-SQL Commands? - Interview Question of the Week #091 timedelay-800x308

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.

How to Introduce Time Delay Between Two T-SQL Commands? - Interview Question of the Week #091 delayintime

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.

Reference: Pinal Dave (https://darkslategrey-bat-805937.hostingersite.com)

SQL DateTime, SQL Function, SQL Server
Previous Post
SQL SERVER – How to Find Out When Your SQL Login Will Expire?
Next Post
What is a Backup Timeline for Restore Process? – Interview Question of the Week #092

Related Posts

Leave a Reply