SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to use SET NOCOUNT ON to increase performance?

SET NOCOUNT ON prevents the sending of progress messages to the client for each statement in a stored procedure or a script. When SQL Server 2005 queries executed, the results prevent [ X rows affected] message from being displayed at the end Transact-SQL statements such as SELECT, INSERT, UPDATE, and DELETE. For some stored procedures or scripts that contain several statements that do not return much actual data, this may provide a significant performance boost because network traffic between the server and client is reduced to a degree. Example below for using SET NOCOUNT ON in a stored procedure and in the execution script.


USE AdventureWorks;
GO
IF OBJECT_ID ( 'Production.GetProductListBelowMaxPrice', 'P' ) IS NOT NULL
DROP PROCEDURE Production.GetProductListBelowMaxPrice;
GO
CREATE PROCEDURE Production.GetProductListBelowMaxPrice
@Product varchar(40)
, @PriceLimit smallmoney
, @Items int OUTPUT
, @ListPrice smallmoney OUT
AS
BEGIN
SET NOCOUNT ON;

SELECT p.name AS ProductName,
'$'+convert(varchar,p.ListPrice,1) AS 'List Price'
FROM Production.Product p
JOIN Production.ProductSubcategory ps
ON p.ProductSubcategoryID = ps.ProductSubcategoryID
WHERE ps.name LIKE @Product AND p.ListPrice < @PriceLimit
ORDER BY [List Price] desc;
SET @Items = @@Rowcount
SET @ListPrice = (SELECT MAX(p.ListPrice)
FROM Production.Product p
JOIN Production.ProductSubcategory ps
ON p.ProductSubcategoryID = ps.ProductSubcategoryID
WHERE ps.name LIKE @Product AND p.ListPrice < @PriceLimit);
RETURN 1
END
GO

/*********** EXECUTION SCRIPT ***************/
SET NOCOUNT ON;
DECLARE @RC int
DECLARE @Product varchar(40)
DECLARE @PriceLimit smallmoney
DECLARE @Items int
DECLARE @ListPrice smallmoney

SET @Product = 'Road%'
SET @PriceLimit = 1000.0

EXECUTE @RC = [AdventureWorks].[Production].[GetProductListBelowMaxPrice]
@Product
,@PriceLimit
,@Items OUTPUT
,@ListPrice OUTPUT

SELECT ReturnFlag = @RC, Items=@Items, LimitPrice=@PriceLimit, MaxPriceReturned=@ListPrice
GO

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.