在 SQL Server 中禁用打印 [英] Disable PRINT in SQL Server
问题描述
我有一个包含许多调试消息的脚本,这些消息由 PRINT
函数打印出来.有什么办法可以禁用这些消息吗?我想到了类似 SET NOCOUNT ON
的东西,但对于用户消息.
I have a script with many debug messages, which are printed by PRINT
function. Is there any way to disable that messages? I have in mind something like SET NOCOUNT ON
, but for user messages.
推荐答案
我喜欢在我的脚本/SP 中设置一个变量 @Debug tinyint.
I like to set a variable, @Debug tinyint in my scripts/SPs.
默认/设置为 0 以抑制消息,1 以显示消息.
Default/set this to 0 to suppress messages, 1 to show messages.
然后不要使用 PRINT,而是使用:
Then instead of using PRINT, use:
IF @Debug > 0 RAISERROR( 'Busy doing something...', 0, 1 ) WITH NOWAIT
使用 WITH NOWAIT 强制立即显示消息,而不仅仅是在刷新输出缓冲区时.
Using WITH NOWAIT forces the message to be displayed immediately and not just when the output buffer is flushed.
作为惯例,我使用@Debug = 1 表示进度消息,@Debug = 2 包含动态 SQL,@Debug = 3 输出结果集.
As a convention, I use @Debug = 1 for progress messages, @Debug = 2 to include dynmaic SQL, @Debug = 3 to output result sets.
当然,如果您的脚本中有 GO 批处理终止符,变量方法将不起作用.
Of course if you have GO batch terminators in your script the variable method won't work.
这篇关于在 SQL Server 中禁用打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!