Wednesday, June 21, 2006

Timed T-SQL Statements Execution

Many a time we would want to execute some SQL Statement or call a stored procedure at specific point in time or after some specified amount of time. There are numerous ways to solve this problem - One of the easiest way is to use the WAITFOR TIME / DELAY statement.

Example 1: The following SELECT statement would start executing only at 11:34:36 AM

WAITFOR TIME '11:34:36'
BEGIN
SELECT * FROM
END
GO

Example 2: The following SELECT statement would start executing after 10 minutes from the time of execution.

WAITFOR DELAY '00:10:00'
BEGIN
SELECT * FROM
END
GO