请参阅以下代码:
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语句,如下所示:How do I flush the PRINT buffer in TSQL?,但所有语句仍然出现在最后。
答案 0 :(得分:1)
使用RAISERROR严重性为0到10的组合和WITH NOWAIT子句,用于立即将输出发送到Messages窗口的语句。
RAISERROR ('Now that''s what I call a message!', 0, 1) WITH NOWAIT