March 03, 2008

Execute a T-SQL statement at a given time




The 'WAITFOR' command is used to delay the execution of a batch, stored procedure or transaction till a specified time duration or till an actual time. Let me demonstrate this:

Create a DELAY for a specific amount of time

USE NORTHWIND
WAITFOR DELAY '00:01:00'
BEGIN
SELECT CustomerID, CompanyName, ContactName FROM CUSTOMERS
END

Delays the execution of the T-Sql statement by 1 minute. To delay by an hour, you would use '01:00:00'. You can specify a maximum of 24 hours.

Execute at the given time (actual time)

USE NORTHWIND
WAITFOR TIME '11:23:00'
BEGIN
SELECT CustomerID, CompanyName, ContactName FROM CUSTOMERS
END

Delays the execution of the T-Sql statement till the time '11:23 A.M'. You cannot specify a date, only time is allowed.


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

1 Response to "Execute a T-SQL statement at a given time"
  1. SQL query Faqs said...
    August 5, 2008 at 12:20 PM

    Nice tutorial. You can find more about SQL queries at

    SQL Query Question Answers

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions