SQL Server WAITFOR Delay

Source: Internet
Author: User
Tags datetime

In the help of SQL server2008, there is an example of WAITFOR Delay, which is incorrect !!!
 
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 ('1970-01-01' + @ DelayLength + '. 000') = 0
BEGIN
SELECT @ ReturnInfo = 'invalidtime' + @ 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
 

 
If you run the code above, an error is returned.
Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.
(Note that the error is on the WAITFOR line .)

 
No way to use the time variable type !!!
Declare @ t time;
Set @ t = '00: 00: 01 ';
Waitfor delay @ t;

 
Actual Results
Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.
(Note that the error is on the WAITFOR line .)

 
 
If constants are used directly, there is no problem. Pass !!
Waitfor delay '00: 00: 10'
 
If you want to use a variable, you can only write it like this. You need to define it as the datatime type, and then use a date at will, followed by the time you need to delay:
Declare @ t datetime;
Set @ t = '1970-01-01 00: 00: 60'; -- delay: 60 seconds
Waitfor delay @ t;
 
The 2007-01-01 in the above code does not have any practical significance, and any date can be used. But the date is required.
 
If you write as follows,
Declare @ t datetime;
Set @ t = '00: 00: 60'; -- delay: 60 seconds
Waitfor delay @ t;

No error will be reported during compilation and running, but it will not delay the specified time (60 seconds), that is, to execute the next sentence without delay .. The cause is unknown. It may be a SQL server bug.
 
 
---------------------------------------
 
Steps to Reproduce
Declare @ t time;
Waitfor delay @ t;

-- Or:
Declare @ t time;
Set @ t = '00: 00: 01 ';
Waitfor delay @ t;

Actual Results
Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.
(Note that the error is on the WAITFOR line .)


Expected Results
At least a better error. The user has not supplied a character string, so the error shocould not mention character strings.

Ideally, these shocould work just like they do with datetime. The [time] type is a natural choice for waitfor delay.

The documentation doesn' t help too much. while it suggests using [datetime] instead of [time], it says confusingly that "Dates cannot be specified; therefore, the date part of the datetime value is not allowed. "However, this works:

Declare @ t datetime;
Set @ t = '2017-01-01 ';
Waitfor delay @ t;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.