📅  最后修改于: 2023-12-03 15:35:05.437000             🧑  作者: Mango
SQL Waitfor is a command that is used to delay the execution of a SQL statement or a batch of statements for a specified amount of time or until a specific time. This can be useful when coordinating processes or waiting for resources to become available.
WAITFOR DELAY 'time'
WAITFOR TIME 'time'
DELAY
- Specifies the amount of time to wait before executing the next SQL statement or batch of statements. The time is specified in the format HH:MI:SS.mmm
, where HH
represents hours, MI
represents minutes, SS
represents seconds, and mmm
represents milliseconds. For example, WAITFOR DELAY '00:00:05.000'
will delay for 5 seconds.
TIME
- Specifies the exact time to wait for before executing the next SQL statement or batch of statements. The time is specified in the format HH:MI:SS.mmm
, where HH
represents hours, MI
represents minutes, SS
represents seconds, and mmm
represents milliseconds. For example, WAITFOR TIME '23:59:59.999'
will wait until the end of the day before executing the next statement.
SELECT 'Start'
WAITFOR DELAY '00:00:05.000'
SELECT 'End'
This code will display the message 'Start', wait for 5 seconds, and then display the message 'End'.
SELECT 'Start'
WAITFOR TIME '23:59:59.999'
SELECT 'End'
This code will display the message 'Start' and then wait until the end of the day before displaying the message 'End'.
SQL Waitfor is a useful command for coordinating processes and waiting for resources to become available. By delaying the execution of SQL statements or waiting for a specific time, you can ensure that your SQL code runs smoothly and efficiently.