看msdn:
语法为:
waitfor
{
delay 'time_to_pass'
| time 'time_to_execute'
| [ ( receive_statement ) | ( get_conversation_group_statement ) ]
[ , timeout timeout ]
}
以下示例在晚上 10:20 (22:20
) 执行存储过程 sp_update_job
。
复制代码 代码如下:
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小时
复制代码 代码如下:
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语句。
邀月注:本文版权由邀月和博客园共同所有,转载请注明出处。