How to use the PRINT statement to track execution as stored procedure is running?

前端 未结 3 788
鱼传尺愫
鱼传尺愫 2020-12-30 01:49

Reference: SQL Server

I have a stored procedure with a while loop in it and I want some messages to be printed after every 500 loops.

So, I\'ve

3条回答
  •  青春惊慌失措
    2020-12-30 02:40

    SQL Server returns messages after a batch of statements has been executed. Normally, you'd use SQL GO to indicate the end of a batch and to retrieve the results:

    PRINT '1'
    GO
    
    WAITFOR DELAY '00:00:05'
    
    PRINT '2'
    GO
    
    WAITFOR DELAY '00:00:05'
    
    PRINT '3'
    GO
    

    In this case, however, the print statement you want returned immediately is in the middle of a loop, so the print statements cannot be in their own batch. The only command I know of that will return in the middle of a batch is RAISERROR (...) WITH NOWAIT, which gbn has provided as an answer as I type this.

提交回复
热议问题