DF207: Data-returning SELECT statement is used within the trigger.

Last modified: December 25, 2024

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

Category

PERFORMANCE

Message

Data-returning SELECT statement is used within the trigger.

Description

Triggers should not return data to the client, as this can lead to unexpected consequences.

Additional information

It is important to note that using a data-returning SELECT statement within a trigger can have performance implications. Each time the trigger fires, the SELECT statement executes, potentially impacting the overall performance of the database, especially if the SELECT statement retrieves a large amount of data or is executed frequently. Additionally, data-modifying triggers can be affected by the use of data-returning SELECT statements, potentially leading to unexpected behavior or performance issues.

Noncompliant code example

CREATE OR ALTER trigger [labour].[trg_workout]
ON [labour].[workout]
AFTER INSERT, UPDATE
AS
BEGIN
  SET NOCOUNT ON
  SELECT id FROM dbo.demotable
  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
    SET NOCOUNT ON
 
    IF EXISTS(SELECT * FROM inserted WHERE Time < 0) ROLLBACK;
END;
GO