DF206: The PRINT statement is used in the trigger.

Last modified: December 25, 2024

The topic describes the DF206 T-SQL code analysis rule.

Category

PERFORMANCE

Message

The PRINT statement is used in the trigger.

Description

Triggers should not return messages to the client.

Aadditional information

Triggers should not return messages to the client to maintain transaction integrity and performance. Triggers are designed to enforce business rules and maintain data consistency within the database. Returning messages can disrupt this process, create unnecessary overhead, and lead to unexpected client behavior. Instead, errors and business rules should be enforced by causing transactions to fail, allowing the application layer to handle messaging and user interactions. This separation of concerns ensures a clear distinction between data management and client communication, which enhances security and efficiency.

Noncompliant code example

CREATE OR ALTER TRIGGER [Labour].[trg_WorkOut]
ON [Labour].[WorkOut]
AFTER INSERT, UPDATE
AS BEGIN
    PRINT 'In Trigger';
    IF EXISTS(SELECT * FROM inserted WHERE Time < 0) ROLLBACK;
END;
GO

Compliant solution

CREATE OR ALTER TRIGGER [Labour].[trg_WorkOut]
ON [Labour].[WorkOut]
AFTER INSERT, UPDATE
AS BEGIN
 
    IF EXISTS(SELECT * FROM inserted WHERE Time < 0) ROLLBACK;
END;
GO