Introduction to SQL Server 2008 WAITFOR Usage _mssql2008

Source: Internet
Author: User

See MSDN:
Http://msdn.microsoft.com/zh-cn/library/ms187331.aspx

The syntax is:


{



[, TIMEOUT TIMEOUT]
}

The following example executes a stored procedure at night 10:20 ( 22:20 ) sp_update_job .

Copy Code code as follows:

Use msdb;
EXECUTE sp_add_job @job_name = ' testjob ';
BEGIN
WAITFOR time ' 22:20 ';
EXECUTE sp_update_job @job_name = ' testjob ',
@new_name = ' updatedjob ';
End;
Go

The following example executes a stored procedure after a two-hour delay. Note: Delay is no more than 24 hours
Copy Code code as follows:

BEGIN
WAITFOR DELAY ' 02:00 ';
EXECUTE sp_helpdb;
End;
Go

The following example shows how to use local variables for the WAITFOR DELAY option. A stored procedure is created that waits for a variable period of time and then returns the elapsed hours, minutes, and seconds information to the user.
Copy Code code as follows:

Use ADVENTUREWORKS2008R2;
Go
IF object_id (' dbo. Timedelay_hh_mm_ss ', ' P ') is not NULL
DROP PROCEDURE dbo. TIMEDELAY_HH_MM_SS;
Go
CREATE PROCEDURE dbo. Timedelay_hh_mm_ss
(
@DelayLength char (8) = ' 00:00:00 '
)
As
DECLARE @ReturnInfo varchar (255)
IF ISDATE (' 2000-01-01 ' + @DelayLength + '. 000 ') = 0
BEGIN
SELECT @ReturnInfo = ' Invalid time ' + @DelayLength
+ ', HH:MM:SS, submitted. ';
--This PRINT statement was for testing and not with in production.
PRINT @ReturnInfo
Return (1)
End
BEGIN
WAITFOR DELAY @DelayLength
SELECT @ReturnInfo = ' A total of ' + @DelayLength + ',
HH:MM:SS, has elapsed! Your time are up. '
--This PRINT statement was for testing and not with in production.
PRINT @ReturnInfo;
End;
Go
/* This statement executes the dbo. TIMEDELAY_HH_MM_SS procedure. */
EXEC timedelay_hh_mm_ss ' 00:00:10 ';
Go

Execution Result: A total of 00:00:10, in Hh:mm:ss, has elapsed. Your time are up. Summary: This is a lightweight solution. When you do not have permission to specify a job, consider using the WAITFOR statement.
Invite the Month Note: This article copyright by invite month and the blog Garden Common All, reprint please indicate the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.