除了
WHILE 1 = 1其他答案所建议的以外,我还经常在SQL“ infintie”循环中添加“超时”,如以下示例所示:
DECLARE @startTime datetime2(0) = GETDATE();-- This will loop until BREAK is called, or until a timeout of 45 seconds.WHILE (GETDATE() < DATEADD(SECOND, 45, @startTime))BEGIN -- Logic goes here: The loop can be broken with the BREAK command. -- Throttle the loop for 2 seconds. WAITFOR DELAY '00:00:02';END
我发现上述技术在从长轮询AJAX后端调用的存储过程中很有用。在数据库端进行循环可以使应用程序不必经常访问数据库来检查新数据。



