运行 SQL 命令循环直到到达时间条件
Run SQL command in loop until reach time condition
我需要解决以下问题(在 TSQL 中):
DECLARE
,@CurrentTime AS DATETIME = GETDATE()
,@KillTime AS DATETIME
,@ProcessToKill AS VARCHAR(100)
,@KillProcessCommand AS VARCHAR(100)
SET @KillTime = comming from concrete select
SET @ProcessToKill = process ID comming from concrete select
SET @KillProcessCommand = kill command
例如:
当前时间 = 2022-06-01 16:00:00.830 ; KillTime = 2022-06-01 15:55:00.000
我的循环应该循环进行,直到 CurrentTime >= KillTime - 如果满足,则终止进程,否则每 5 分钟重复一次。
有人可以帮忙吗?
谢谢
不需要运行循环等待一次,直接使用WAITFOR
命令即可,例如
PRINT 'Time start: ' + CONVERT(VARCHAR(8), GETDATE(), 8);
DECLARE @Time VARCHAR(8) = CONVERT(VARCHAR(8), DATEADD(SECOND, 5, GETDATE()), 8);
WAITFOR TIME @Time;
PRINT 'Time End: ' + CONVERT(VARCHAR(8), GETDATE(), 8);
我需要解决以下问题(在 TSQL 中):
DECLARE
,@CurrentTime AS DATETIME = GETDATE()
,@KillTime AS DATETIME
,@ProcessToKill AS VARCHAR(100)
,@KillProcessCommand AS VARCHAR(100)
SET @KillTime = comming from concrete select
SET @ProcessToKill = process ID comming from concrete select
SET @KillProcessCommand = kill command
例如: 当前时间 = 2022-06-01 16:00:00.830 ; KillTime = 2022-06-01 15:55:00.000
我的循环应该循环进行,直到 CurrentTime >= KillTime - 如果满足,则终止进程,否则每 5 分钟重复一次。
有人可以帮忙吗? 谢谢
不需要运行循环等待一次,直接使用WAITFOR
命令即可,例如
PRINT 'Time start: ' + CONVERT(VARCHAR(8), GETDATE(), 8);
DECLARE @Time VARCHAR(8) = CONVERT(VARCHAR(8), DATEADD(SECOND, 5, GETDATE()), 8);
WAITFOR TIME @Time;
PRINT 'Time End: ' + CONVERT(VARCHAR(8), GETDATE(), 8);