Sometimes even the simplest things can make a difference. One of these simple items that should be part of every stored procedure is SET NOCOUNT ON. This one line of code, put at the top of a stored procedure turns off the messages that SQL Server sends back to the client after each T-SQL statement is executed. This is performed for all SELECT, INSERT, UPDATE, and DELETE statements. Having this information is handy when you run a T-SQL statement in a query window, but when stored procedures are run there is no need for this information to be passed back to the client.
By removing this extra overhead from the network it can greatly improve overall performance for your database and application.
If you still need to get the number of rows affected by the T-SQL statement that is executing you can still use the @@ROWCOUNT option. By issuing a SET NOCOUNT ON this function (@@ROWCOUNT) still works and can still be used in your stored procedures to identify how many rows were affected by the statement.
Microsoft even realized the issue that this creates and has changed the stored procedure templates from SQL Server 2000 to SQL Server 2005.
Here is the old template style available in SQL Server 2000 without the SET NOCOUNT ON.
-- =============================================
-- Create procedure basic template
-- =============================================
-- creating the store procedure
IF EXISTS (SELECTname
FROM sysobjects
WHEREname = N'
'
AND type = 'P')
DROPPROCEDURE
GO
CREATEPROCEDURE
<@param1, sysname, @p1> int> = ,
<@param2, sysname, @p2> int> =
AS
SELECT @p1, @p2
GO
-- =============================================
-- example to execute the store procedure
-- =============================================
EXECUTE,
GO
Here is the new template style available in SQL Server 2005 with the SET NOCOUNT ON.
As you can see even simple little things such as this can make an overall improvement for your database environment. Stay tuned for other simple tricks and techniques to improve performance.
When we SET NOCOUNT is ON, the count (indicating the number of rows affected by a T-SQL statement) is not returned. When SET NOCOUNT is OFF, the count is returned. It is used with any SELECT, INSERT, UPDATE, DELETE statement. SET NOCOUNT ON improves stored procedure performance.
geetha velusamyPosted Nov 12, 2021, 1:03 PM
SET NO COUNT ON:
SET NO COUNT is ON the count is not returned.
Sample Query for SET NO COUNT ON:
SET NOCOUNT ON;
GO
SELECT [empid]
,[empname]
,[email]
,[address]
FROM [emp]
SET NO COUNT OFF:
SET NO COUNT is OFF it returns the count of rows affected.
Sample Query for SET NO COUNT OFF:
SET NOCOUNT OFF;
GO
SELECT [empid]
,[empname]
,[email]
,[address]
FROM [emp]
Vinitha TPosted Nov 8, 2021, 1:56 PM
SET NOCOUNT ON - This prevent to display number of row(s) affected and will display as Commands completed successfully in message tab
SET NOCOUNT OFF - This display number of row(s) affected.
Mageshwaran RPosted Nov 5, 2019, 8:58 AM
Subbarao APosted Jul 10, 2019, 3:22 AM
Sivakumar KonetiPosted Jul 9, 2019, 12:48 AM
Rajanikant HawaldarPosted Jul 8, 2019, 3:42 AM
Amit MohantyPosted Jul 8, 2019, 3:26 AM
https://www.c-sharpcorner.com/blogs/set-nocount-on-and-set-nocount-off-using-stored-procedure-in-sql-server
https://www.tutorialgateway.org/sql-set-nocount-on/
https://www.tech-recipes.com/rx/56506/sql-server-set-nocount-on-statement-with-examples/
Madan ShekarPosted Jul 8, 2019, 3:25 AM