Wednesday, 20 November 2013

Use of SET NOCOUNT in stored procedures

Whenever we write any procedure and execute it a message appears in message window that shows number of rows affected with the statement written in the procedure.
But this message creates an extra overhead on the network. By using SET NOCOUNT we can remove this extra overhead from the network, that can actually improve the performance of our database and our application.
When SET NOCOUNT is ON, the count is not returned. When SET NOCOUNT is OFF, the count is returned.
Example:
USE STUDENTDB
GO
SELECT * FROM [STUDENTDB].[STUDENT];
Messege:
(664 row(s) affected)

USE STUDENTDB
GO
SET NOCOUNT ON
SELECT * FROM [STUDENTDB].[STUDENT];
SET NOCOUNT OFF
Messege:
Command(s) completed successfully.
SET NOCOUNT NO statement can be useful in store procedures. SET NOCOUNT ON statement into store procedures can reduce network traffic, because client will not receive the message indicating the number of rows affected by T-SQL statement. Setting SET NOCOUNT to ON can provide a significant performance boost, because network traffic is greatly reduced.
The @@ROWCOUNT function is updated even when SET NOCOUNT is ON.
Reference: http://msdn.microsoft.com/en-us/library/ms189837.aspx

No comments:

Post a Comment