DF209: SET NOCOUNT ON is missing before the first DML statement.
Last modified: December 25, 2024
The topic describes the DF209 T-SQL code analysis rule.
Category
PERFORMANCE
Message
SET NOCOUNT ON is missing before the first DML statement.
Description
It is recommended to use the SET NOCOUNT ON statement at the beginning of a batch of SQL statements to prevent sending unnecessary messages to the client.
Additional information
It’s important to include SET NOCOUNT ON before the first Data Manipulation Language (DML) statement in stored procedures or triggers. This setting suppresses the “xx rows affected” message that SQL Server returns after executing DML statements. Failing to set NOCOUNT ON can lead to increased network traffic and decreased performance, especially in scenarios involving multiple DML statements or large result sets. Additionally, omitting SET NOCOUNT ON may cause compatibility issues with client applications expecting only result sets.
Noncompliant code example
CREATE PROCEDURE dbo.DemoProcedure
AS
BEGIN
SELECT id FROM dbo.DemoTable;
END
GO
Compliant solution
CREATE PROCEDURE dbo.DemoProcedure
AS
BEGIN
SET NOCOUNT ON;
SELECT id FROM dbo.DemoTable;
END
GO