在使用这个语句之前,我们先看看微软官方给的帮助文档里面对Waitfor的说明:

 


 

本文内容

  1. 语法
  2. 参数
  3. 备注
  4. 示例
  5. 另请参阅

Parallel Data Warehouse

Blocks the execution of a batch, stored procedure, or transaction until either a specified time or time interval elapses, or a specified statement modifies or returns at least one row.

Transact-SQL Syntax Conventions

Syntax

syntaxsql
  
WAITFOR   
{  
    DELAY 'time_to_pass'   
  | TIME 'time_to_execute'   
  | [ ( receive_statement ) | ( get_conversation_group_statement ) ]   
    [ , TIMEOUT timeout ]  
}  

Arguments

Is the specified period of time that must pass, up to a maximum of 24 hours, before execution of a batch, stored procedure, or transaction proceeds.

time_to_pass is formatted as hh:mm[[:ss].mss].

Is the specified time when the batch, stored procedure, or transaction runs.

time_to_execute is formatted as hh:mm[[:ss].mss] and can optionally include the date of 1900-01-01.

Is a valid RECEIVE statement.

重要

RECEIVE (Transact-SQL).

Is a valid GET CONVERSATION GROUP statement.

重要

GET CONVERSATION GROUP (Transact-SQL).

Specifies the period of time, in milliseconds, to wait for a message to arrive on the queue.

重要

GET CONVERSATION GROUP (Transact-SQL).

Remarks

While executing the WAITFOR statement, the transaction is running and no other requests can run under the same transaction.

If the server is busy, the thread may not be immediately scheduled, so the time delay may be longer than the specified time.

If a query can't return any rows, WAITFOR will wait forever or until TIMEOUT is reached, if specified.

Cursors can't be opened on WAITFOR statements.

Views can't be defined on WAITFOR statements.

sp_who.

monitors the number of WAITFOR statement threads, and randomly selects some of these threads to exit if the server starts to experience thread starvation.

SQL Server identifies these scenarios and returns an empty result set if the chance of such a deadlock exists.

注意

If necessary, adjust the timeout setting for the connection at the application level.

Examples

Using WAITFOR TIME

(22:20).

复制
 
EXECUTE sp_add_job @job_name = 'TestJob';  
BEGIN  
    WAITFOR TIME '22:20';  
    EXECUTE sp_update_job @job_name = 'TestJob',  
        @new_name = 'UpdatedJob';  
END;  
GO  

Using WAITFOR DELAY

The following example executes the stored procedure after a two-hour delay.

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

Using WAITFOR DELAY with a local variable

This stored procedure waits for a variable period of time and then returns information to the user as the elapsed numbers of hours, minutes, and seconds.

复制
 
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  

Here is the result set.

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

See Also

datetime (Transact-SQL) 

 

 

 

 

出处:https://docs.microsoft.com/zh-cn/sql/t-sql/language-elements/waitfor-transact-sql?view=sql-server-ver15

==============================================================================================

从上面的说明里,我们可以看到  TIMEOUT timeout 是可以精确到毫秒级的。

Datetime 格式的可以精确到毫秒,也就是10的负3次方秒,如对应获取系统时间的方法:getdate()

Datetime2 格式的可以精确到微秒,也就是10的负6次方秒,如对应获取系统时间的方法:sysdatetime()

 

正好,项目上有用到这块的内容,就简单的看看,研究下用法,我们先来测试看看。

 

延迟1毫秒

SELECT getdate()
WAITFOR DELAY   '00:00:00.001'
SELECT getdate()

两次的执行结果分别如下

2008-01-10 22:54:13.513

2008-01-10 22:54:13.513

两次获得的时间完全一样。

延迟两毫秒就能看返回的时间差别

SELECT getdate()
WAITFOR DELAY   '00:00:00.002'
SELECT getdate()

延迟两次的执行结果

2008-01-10 22:58:37.450
2008-01-10 22:58:37.467

由于我用的一个存储过程中需要毫秒级的时间串产生序列号,因为没有延迟,所以会出现序列号相同的问题,所以这个延迟函数就帮了大忙。

SELECT convert(varchar(8),   getdate(),112)  +  replace(convert(varchar(12),getdate(),114),':','') as  NewSerial
WAITFOR DELAY   '00:00:00.002'
SELECT convert(varchar(8),   getdate(),112)  +  replace(convert(varchar(12),getdate(),114),':','') as  NewSerial

执行结果如下

20080110230256640
20080110230256653

 

出处:https://blog.csdn.net/jimlong/java/article/details/5862406

相关文章: