1

请看下面的代码:

USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Test]
AS
BEGIN
begin tran
begin try
    SET NOCOUNT ON;
    DECLARE @ID int
    DECLARE @Count int
    set @Count=0
    DECLARE Employee_Cursor CURSOR FOR
SELECT id
FROM Person3;
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor INTO @ID
WHILE @@FETCH_STATUS = 0
   BEGIN    
        WAITFOR DELAY '000:00:01'
        SET @Count = @Count+1
        Print @Count        
      FETCH NEXT FROM Employee_Cursor INTO @ID;
   END;
   commit tran
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
end try
begin catch
ROLLBACK tran
end catch
END

存储过程运行,然后将以下输出一次全部刷新到客户端(因为在:person3 中有三个记录):

1
2
3

如何在每次游标迭代时将输出刷新到客户端?我正在寻找与 SQL Server 2005 兼容的答案。

我已尝试使用此处描述的 RAISEERROR 语句:如何在 TSQL 中刷新 PRINT 缓冲区?,但是所有语句仍然出现在最后。

4

1 回答 1

1

将 0 到 10 的 RAISERROR 严重性与 WITH NOWAIT 子句组合用于立即将输出发送到消息窗口的语句。

RAISERROR ('Now that''s what I call a message!', 0, 1) WITH NOWAIT

参考:https ://www.mssqltips.com/sqlservertip/1660/using-the-nowait-option-with-the-sql-server-raiserror-statement/

于 2015-08-13T19:58:55.960 回答