sqlsql-servert-sqlnocount

Is "SET NOCOUNT OFF" necessary in a stored procedure?


I have many procedures that has set nocount on.

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

e.g.:

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

Solution

  • 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 will undo this suppression. However, set nocount on is a scope setting, and by default, will be turned off when leaving the scope anyway.

    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.