SQL Server 2008中SQL之WaitFor-在规定时间执行操作

    
 

 

在SQL Server 2005以上版本中,在一个增强的WaitFor命令,其作用可以和一个job相当。但使用更加简捷。

看MSDN:
http://msdn.microsoft.com/zh-cn/library/ms187331.aspx
语法为:

[ruby] view plain copy print ?
  1. WAITFOR
  2. {
  3. DELAY 'time_to_pass'
  4. | TIME 'time_to_execute'
  5. | [ ( receive_statement ) | ( get_conversation_group_statement ) ]
  6. [ , TIMEOUT timeout ]
  7. }
WAITFOR { DELAY 'time_to_pass' | TIME 'time_to_execute' | [ ( receive_statement ) | ( get_conversation_group_statement ) ] [ , TIMEOUT timeout ] }

以下示例在晚上 10:20 (22:20 ) 执行存储过程 sp_update_job 。

[ruby] view plain copy print ?
  1. USE msdb;
  2. EXECUTE sp_add_job @job_name ='TestJob';
  3. BEGIN
  4. WAITFOR TIME '22:20';
  5. EXECUTE sp_update_job @job_name ='TestJob',
  6. @new_name ='UpdatedJob';
  7. END;
  8. GO
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

以下示例在两小时的延迟后执行存储过程。注意:Delay最多不超过24小时

[ruby] view plain copy print ?
  1. BEGIN
  2. WAITFOR DELAY '02:00';
  3. EXECUTE sp_helpdb;
  4. END;
  5. GO
  6. 以下示例显示如何对 WAITFOR DELAY选项使用局部变量。将创建一个存储过程,该过程将等待可变的时间段,然后将经过的小时、分钟和秒数信息返回给用户。
  7. USE AdventureWorks2008R2;
  8. GO
  9. IF OBJECT_ID('dbo.TimeDelay_hh_mm_ss','P') IS NOT NULL
  10. DROP PROCEDURE dbo.TimeDelay_hh_mm_ss;
  11. GO
  12. CREATE PROCEDURE dbo.TimeDelay_hh_mm_ss
  13. (
  14. @DelayLength char(8)='00:00:00'
  15. )
  16. AS
  17. DECLARE @ReturnInfo varchar(255)
  18. IF ISDATE('2000-01-01 ' +@DelayLength +'.000') = 0
  19. BEGIN
  20. SELECT @ReturnInfo ='Invalid time ' +@DelayLength
  21. + ',hh:mm:ss, submitted.';
  22. -- This PRINT statement is for testing,not usein production.
  23. PRINT @ReturnInfo
  24. RETURN(1)
  25. END
  26. BEGIN
  27. WAITFOR DELAY @DelayLength
  28. SELECT @ReturnInfo ='A total time of ' +@DelayLength + ',
  29. hh:mm:ss, has elapsed! Your time is up.'
  30. -- This PRINT statement is for testing,not usein production.
  31. PRINT @ReturnInfo;
  32. END;
  33. GO
  34. /* This statement executes the dbo.TimeDelay_hh_mm_ss procedure. */
  35. EXEC TimeDelay_hh_mm_ss '00:00:10';
  36. GO
BEGIN WAITFOR DELAY '02:00'; EXECUTE sp_helpdb; END; GO 以下示例显示如何对 WAITFOR DELAY选项使用局部变量。将创建一个存储过程,该过程将等待可变的时间段,然后将经过的小时、分钟和秒数信息返回给用户。 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


执行结果:

A total time of 00:00:10, in hh:mm:ss, has elapsed.Your time is up.

小结:这是一种轻巧的解决方案。当你没有权限指定job时,可以考虑用WaitFor语句。

邀月注:本文版权由邀月和CSDN共同所有,转载请注明出处。
助人等于自助! [email protected]

你可能感兴趣的:(sql,sql,server,存储,Ruby,delay,testing)