Skip to content

Instantly share code, notes, and snippets.

@ritacse
Last active February 18, 2025 14:41
Show Gist options
  • Save ritacse/b35941df80c620a76dd00b4844b4897f to your computer and use it in GitHub Desktop.
Save ritacse/b35941df80c620a76dd00b4844b4897f to your computer and use it in GitHub Desktop.
SET NOCOUNT ON;
--It's often used at the beginning of stored procedures and triggers.
--Benefits of SET NOCOUNT ON
--1.Reduces network traffic:
--2.Reduces processing
--Example:
ALTER PROCEDURE [dbo].[CheckUserMenuPermission]
@UserId INT,
@ControllerName NVARCHAR(100),
@ActionResult NVARCHAR(100)
AS
BEGIN
SET NOCOUNT ON; --// ** here it is
SELECT * FROM Menu_Permission_T MP INNER JOIN Menu_T M ON MP.MenuID=M.ID
where MP.UserID= @UserId and URL = '/'+@ControllerName+'/'+ @ActionResult
END
-- Essentially, "NOCOUNT" controls message display,
-- while "NOLOCK" affects data consistency by bypassing locking mechanisms.
SELECT * FROM Menu_Permission_T MP WITH (NOLOCK)
Example Scenarios:
-----------------
Use "SET NOCOUNT":
When executing a series of updates within a stored procedure where the actual number of rows modified is not critical to the application logic.
Use "NOLOCK":
When running a read-only query against a large table that is frequently updated, and you can tolerate potentially reading uncommitted changes for faster result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment