看MSDN:
http://msdn.microsoft.com/zh-cn/library/ms187331.aspx
語(yǔ)法為:
WAITFOR
{
DELAY 'time_to_pass'
| TIME 'time_to_execute'
| [ ( receive_statement ) | ( get_conversation_group_statement ) ]
[ , TIMEOUT timeout ]
}
以下示例在晚上 10:20 (22:20
) 執(zhí)行存儲(chǔ)過(guò)程 sp_update_job
。
復(fù)制代碼 代碼如下:
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
以下示例在兩小時(shí)的延遲后執(zhí)行存儲(chǔ)過(guò)程。注意:Delay最多不超過(guò)24小時(shí)
復(fù)制代碼 代碼如下:
BEGIN
WAITFOR DELAY '02:00';
EXECUTE sp_helpdb;
END;
GO
以下示例顯示如何對(duì) WAITFOR DELAY 選項(xiàng)使用局部變量。將創(chuàng)建一個(gè)存儲(chǔ)過(guò)程,該過(guò)程將等待可變的時(shí)間段,然后將經(jīng)過(guò)的小時(shí)、分鐘和秒數(shù)信息返回給用戶。
復(fù)制代碼 代碼如下:
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 is for testing, not use in production.
PRINT @ReturnInfo
RETURN(1)
END
BEGIN
WAITFOR DELAY @DelayLength
SELECT @ReturnInfo = 'A total time of ' + @DelayLength + ',
hh:mm:ss, has elapsed! Your time is up.'
-- This PRINT statement is for testing, not use 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
執(zhí)行結(jié)果:A total time of 00:00:10, in hh:mm:ss, has elapsed.Your time is up.小結(jié):這是一種輕巧的解決方案。當(dāng)你沒(méi)有權(quán)限指定job時(shí),可以考慮用WaitFor語(yǔ)句。
邀月注:本文版權(quán)由邀月和博客園共同所有,轉(zhuǎn)載請(qǐng)注明出處。