WAITFOR TIME and WAITFOR DELAY

Posted on 2007-05-28 15:02 my 阅读(1280) 评论(0)  编辑  收藏 所属分类: 数据库方面
 

A. 使用 WAITFOR TIME

以下示例在晚上 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

B. 使用 WAITFOR DELAY

以下示例在两小时的延迟后执行存储过程。

BEGIN
WAITFOR DELAY '02:00';
EXECUTE sp_helpdb;
END;
GO

C. 对 WAITFOR DELAY 使用局部变量

以下示例显示如何对 WAITFOR DELAY 选项使用局部变量。将创建一个存储过程,该过程将等待可变的时间段,然后将经过的小时、分钟和秒数信息返回给用户。

USE AdventureWorks;
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.

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 63, comments - 45, trackbacks - 0, articles - 99

Copyright © my