Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 428
dance2die Avatar asked Nov 21 '11 19:11

dance2die


People also ask

Why do we use set Nocount on in stored procedure?

SET NOCOUNT ON prevents the sending of DONEINPROC messages to the client for each statement in a stored procedure.

Should I set Nocount on?

Using SET NOCOUNT ON can improve performance because network traffic can be reduced. SET NOCOUNT ON prevents SQL Server from sending DONE_IN_PROC message for each statement in a stored procedure or batch of SQL statements.

What is the use of set Nocount on off statement?

SET NOCOUNT ON/OFF statement controls the behavior in SQL Server to show the number of affected rows in the T-SQL query.

What is the use of no count?

SET NOCOUNT ON is a set statement which prevents the message which shows the number of rows affected by T-SQL query statements. This is used within stored procedures and triggers to avoid showing the affected rows message.


2 Answers

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.

like image 198
Jerad Rose Avatar answered Oct 04 '22 18:10

Jerad Rose


I know this is a rather old post but it was the first hit on Google when I looked for the answer. The response above to test it was a very good idea.

I tested this out and wanted to update the above with some additional details.

The scope you create with a SET NOCOUNT ON flows to any procs which your procedure calls. So if your procedure does SET NOCOUNT ON and then you call a sproc, that sproc gets your SET NOCOUNT setting. The setting DOES go away when you exit your sproc but the setting flows down into called sprocs. If you SET NOCOUNT inside of the CALLED sproc, the outer sproc will have the SET NOCOUNT which it set and the inner sproc won't affect the outer sproc.

So I think you don't really need to reset it at the end of your sproc because your settings will never flow OUT of your sproc upwards; however, if your sproc depends on the setting, it should set it before it needs it because if it gets called from another sproc, it could have a different setting than you assume.

like image 20
John Mitchell Avatar answered Oct 04 '22 20:10

John Mitchell