Go to DotNetFunda.com
 Online : 1326 |  Welcome, Guest!   Login
 
Home > Articles > Sql Server > SET NOCOUNT (Transact-SQL)

  • Nominate yourself for "Agile Software Development using Scrum" online session for FREE, click here (Hurry, limited seats).

  • Download OOPS and ASP.NET Online training session video and PPT from here.

Submit Article | Articles Home | Search Articles |

SET NOCOUNT (Transact-SQL)

 Posted on: 9/14/2009 3:10:03 AM by Virendradugar | Views: 2337 | Category: Sql Server | Level: Beginner | Print Article
This article explains the SET NOCOUNT keyword's importance.

Ask all your .NET related questions/clarifications here to get quicker solution.

Introduction

Whenever we write any procedure and execute it a message appears in message window that shows no of rows affected with the statement written in the procedure and we become very happy to see that our procedure is working. 

But do you know that this message creates an extra overhead on the network? Yes it does.

By removing this extra overhead from the network, we can actually improve the performance of our database and our application.

How should we do it?

Solution

When you create any procedure then first line of your procedure should be
SET NOCOUNT ON;

This one line of code turns off the message that SQL server sends back to front end after every T-SQL statement is executed. This is applied for all SELECT, INSERT, UPDATE and DELETE statements. As when stored procedures are executed there is no need to pass this information back to front end.

When SET NOCOUNT is ON, the count is not returned. When SET NOCOUNT is OFF, the count is returned.If we still need to get the count of no of rows affected, we can still use @@ROWCOUNT option. Because The @@ROWCOUNT function is updated even when SET NOCOUNT is ON.

The setting specified by SET NOCOUNT is in effect at execute or run time and not at parse time.

Microsoft even realized the issue that this creates and has changed the stored procedure templates from SQL Server 2000 to SQL Server 2005.

Template used in SQL SERVER 2005

================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
– =============================================
– Author: 
– Create date: 
– Description: 
– =============================================
CREATE PROCEDURE 
– Add the parameters for the stored procedure here
<@Param1, sysname, @p1> = ,
<@Param2, sysname, @p2> = 
AS
BEGIN
– SET NOCOUNT ON added to prevent extra result sets from
– interfering with SELECT statements.
SET NOCOUNT ON;
– Insert statements for procedure here
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
================================================

Setting SET NOCOUNT to ON can provide a significant performance boost, because network traffic is greatly reduced.


Conclusion

So next time, when you create any procedure, don't forget to include "SET NOCOUNT ON;". Otherwise, the best pratice is to get the stored procedure template from the SQL Server itself.

Enjoy..


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:3 year(s)
Home page:http://virendrablogs.blogspot.com
Member since:Tuesday, August 11, 2009
Level:Silver
Status: [Member]
Biography:Virendra Dugar is experienced software developer with 3 years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University in india.In free time, he loves to listen music, read books, play games and do blogging etc.

Visit his blog :
http://virendrablogs.blogspot.com
http://virendradugar.wordpress.com
 Latest post(s) from Virendradugar

   ◘ Set Page Position after asyncPostback in AJAX posted on 1/4/2010 1:46:02 AM
   ◘ Common misconception regarding ViewState and TextBox posted on 1/1/2010 8:00:02 AM
   ◘ Starting with JQuery posted on 11/22/2009 9:09:22 AM
   ◘ Copy DLL from GAC posted on 11/14/2009 4:05:08 AM
   ◘ Handle back button of the browser posted on 11/4/2009 5:04:33 AM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)