是“SET NOCOUNT OFF"吗?需要在存储过程中吗?

2021-09-10 00:00:00 sql tsql sql-server nocount

我有很多set nocount on的程序.

是否需要在存储过程结束时将其关闭?

Is it necessary to turn it off at the end of stored procedure?

例如:

create procedure DummyProc
as
begin
    set nocount on
    ...
    set nocount off
end

推荐答案

set nocount on 将禁用 X 行受影响. 消息 SQL 返回.在某些情况下,由于客户端执行存储过程的不良影响,此消息被抑制.

set nocount on will disable the X rows affected. message SQL returns. This message is suppressed, in some cases, due to undesired effects with the client executing the stored proc.

set nocount off 将取消此抑制.但是,set nocount on 是一个范围设置,默认情况下,无论如何离开范围时都会关闭.

set nocount off will undo this suppression. However, set nocount on is a scope setting, and by default, will be turned off when leaving the scope anyway.

现在,是否需要set nocount off?不,因为执行的任何新命令都将在不同的范围内,并且默认情况下 set nocount off 始终有效.但正如上面评论中所述,这被认为是一种很好的做法,只是明确表明该设置将在 proc 执行完成后恢复正常.

Now, is set nocount off necessary? No, as any new commands executed will be in a different scope, and by default set nocount off is always in effect. But as stated above in comments, it's considered a good practice, just to explicitly indicate that this setting will return to normal when the proc is finished executing.

相关文章