在 SSMS 中执行期间的 T-SQL 输出消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1287878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:14:00  来源:igfitidea点击:

T-SQL Output Message During execution in SSMS

sqlsql-server

提问by Chris Klepeis

I have a simple query which loops and I want to see the PRINT messages during the execution. The query is something like this:

我有一个简单的循环查询,我想在执行过程中查看 PRINT 消息。查询是这样的:

WHILE 1 = 1
BEGIN
    WAITFOR DELAY '000:00:10'
    PRINT 'here'
END

The PRINT 'here' does not output until I stop the process, however, I want to see it while its running. Is this possible?

PRINT 'here' 在我停止进程之前不会输出,但是,我想在它运行时看到它。这可能吗?

采纳答案by KM.

I believe that the prints get buffered, releasing "chunks" as the buffer fills up.

我相信打印会被缓冲,当缓冲区填满时会释放“块”。

try using raiserror:

尝试使用 raiserror:

How do I flush the PRINT buffer in TSQL?

如何在 TSQL 中刷新 PRINT 缓冲区?

回答by Mike Forman

You can use RAISERROR with serverity 0 and the NOWAIT option

您可以将 RAISERROR 与 serverity 0 和 NOWAIT 选项一起使用

WHILE 1 = 1
BEGIN
    WAITFOR DELAY '000:00:10'
    RAISERROR ('here', 0, 1) WITH NOWAIT
END

回答by ambrul

Try this..

尝试这个..

DECLARE @i INT = 1

WHILE ( @i <= 10)

BEGIN 

--- do something

SELECT 'Completed  ' + CAST(@i AS VARCHAR(50)) + '  :  ' + CAST(GETDATE() AS VARCHAR(50));

SET @i = @i + 1

END