Skip to content

SA0110 : Avoid have stored procedure that contains IF statements

Avoid using conditional logic in stored procedures, functions, and triggers to prevent optimizer confusion and parameter sniffing issues.

Conditional logic in SQL Server stored procedures, functions, and triggers can lead to issues with the SQL query optimizer. This problem arises because the presence of IF or IF..ELSE statements can obscure the optimal execution plan selection, which may result in inefficient query performance.

For example:

-- Example of problematic use of conditional logic within a stored procedure
CREATE PROCEDURE SampleProcedure
AS
BEGIN
IF (@SomeCondition = 1)
BEGIN
SELECT * FROM TableA;
END
ELSE
BEGIN
SELECT * FROM TableB;
END
END

This example illustrates how conditional logic can lead to inconsistent execution plans being chosen during runtime, impacting performance. The SQL Server optimizer may not be able to accurately determine the best execution path, especially if parameters vary during calls.

  • Leads to performance problems due to suboptimal execution plans.

  • Causes parameter sniffing issues where cached plans may not suit all execution scenarios.

Refactor stored procedures to minimize the use of conditional logic, enhancing performance by ensuring optimal execution plans and reducing parameter sniffing issues.

Follow these steps to address the issue:

1.Identify portions of the stored procedure that contain conditional logic, especially those using IF or IF..ELSE statements.

2.Separate the conditional logic into distinct stored procedures. Each branch of the condition should be placed in its own stored procedure tailored to handle specific logic.

3.In the “main” stored procedure, replace the conditional blocks with calls to these new stored procedures based on condition evaluation.

For example:

-- Original procedure with conditional logic
-- CREATE PROCEDURE SampleProcedure
-- AS
-- BEGIN
-- IF (@SomeCondition = 1)
-- BEGIN
-- SELECT * FROM TableA;
-- END
-- ELSE
-- BEGIN
-- SELECT * FROM TableB;
-- END
-- END
-- Refactored approach
CREATE PROCEDURE SampleProcedure
AS
BEGIN
IF (@SomeCondition = 1)
BEGIN
EXEC SpecificProcedureForCondition1;
END
ELSE
BEGIN
EXEC SpecificProcedureForCondition2;
END
END;
CREATE PROCEDURE SpecificProcedureForCondition1
AS
BEGIN
SELECT * FROM TableA;
END;
CREATE PROCEDURE SpecificProcedureForCondition2
AS
BEGIN
SELECT * FROM TableB;
END;

The rule has a Batch scope and is applied only on the SQL script.

Name Description Default Value
IgnoreIfStatementsWhichDoNotContainDmlOrDddlStaetmetns Ignore IF statements which do not contain any DML or DDL statement. yes

The rule does not need Analysis Context or SQL Connection.

1 hour per issue.

Design Rules, Bugs

There is no additional info for this rule.

CREATE PROCEDURE testsp_SA0110
(
@Code VARCHAR(30) = NULL
)
AS
IF @Code IS NULL
SELECT * FROM Table1
ELSE
SELECT * FROM Table1 WHERE Code like @Code + '%'
  Message Line Column
1 SA0110 : Avoid have stored procedure that contains IF statements. 7 0

Analysis Rules