循环计数器运行非常慢

时间:2016-03-09 22:50:11

标签: sql loops

DECLARE @LoopCounter INT = 1
WHILE(@LoopCounter < 1000)

BEGIN

SELECT account, SUM(amt_paid) FROM transactions
WHERE account = '49944928'
    GROUP BY account
        HAVING SUM(amt_paid) <> 0.00
            COMPUTE SUM (SUM(amt_paid))
SET @LoopCounter = @LoopCounter + 1
WAITFOR DELAY '00:00:05'

END

我试图让它每5秒返回一次结果,但它运行大约5分钟,然后返回结果大约100次,然后返回加载下4或5分钟的结果。

无论如何,我可以让这个运行更快一点,所以它实际上每隔5秒显示一次结果?

1 个答案:

答案 0 :(得分:0)

SET NOCOUNT ON
GO
DECLARE @iteration AS INT
SET @iteration = 1
WHILE(@iteration<=10)
BEGIN
    SELECT 'Start of Iteration ' + CAST(@iteration AS VARCHAR)
    WAITFOR DELAY '00:00:01'
    PRINT 'End Of Iteration ' + CAST(@iteration AS VARCHAR)
    SET @iteration+=1   

    RAISERROR('',10,1) WITH NOWAIT
END