SA0144 : The code following the RETURN or the RAISERROR statements will never be executed
Introduction
Section titled “Introduction”Dead code in T-SQL scripts, which is never executed, can cause inefficiencies and increase confusion in code maintenance.
Description
Section titled “Description”The issue of dead code arises when parts of your T-SQL scripts are included but will never be executed, leading to inefficiencies and confusion.
In the context of T-SQL and SQL Server, dead code can occur in stored procedures, functions, and triggers. It often results from unconditional RETURN statements or RAISERROR commands with severity levels above 10, especially when placed inside BEGIN/END TRY blocks.
For example:
-- Example of dead codeCREATE PROCEDURE [dbo].[checkForDeadCode]( @InputValue BIGINT)ASIF (@InputValue = 0)BEGIN RETURN 0;ENDELSEBEGIN RETURN @InputValue + 1;ENDRETURN -1;PRINT 'This message will never be printed';This example illustrates a common issue where the PRINT statement is unreachable because the procedure always exits before reaching it. This kind of dead code can mislead developers and complicate maintenance.
-
Unreachable code segments can cause confusion during debugging and reviewing, leading to misunderstandings about the logic flow.
-
Including unnecessary code increases script complexity, which can negatively impact performance, especially in systems where maintaining efficiency is critical.
How to fix
Section titled “How to fix”Remove any dead code from your T-SQL scripts to optimize efficiency and clarity.
Follow these steps to address the issue:
1.Review your T-SQL script to identify code segments that cannot be reached due to logical pathways, such as those interrupted by RETURN statements or RAISERROR commands with a severity level above 10 within BEGIN/END TRY blocks.
2.Analyze each identified unreachable section to determine if they provide necessary logic or alerts that are being incorrectly bypassed.
3.If the code is genuinely redundant and not required for execution, remove these segments to improve script maintainability and performance.
For example:
-- Before removing dead codeCREATE PROCEDURE [dbo].[checkForDeadCode]( @InputValue BIGINT)ASIF (@InputValue = 0)BEGIN RETURN 0;ENDELSEBEGIN RETURN @InputValue + 1;ENDRETURN -1;PRINT 'This message will never be printed'; -- Dead code example-- After removing dead codeCREATE PROCEDURE [dbo].[checkForDeadCode]( @InputValue BIGINT)ASIF (@InputValue = 0)BEGIN RETURN 0;ENDELSEBEGIN RETURN @InputValue + 1;END-- Removed unreachable PRINT statementThe rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”20 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
Additional Information
Section titled “Additional Information”Example Test SQL
Section titled “Example Test SQL”ALTER PROCEDURE [dbo].[test_DeadCode_Rule](@Param1 INT = 0)ASBEGIN
BEGIN TRY RAISERROR('@Param1 should not be null',11, 111); PRINT 'dead code' END TRY BEGIN CATCH SET @Param1 = 1 END CATCH
IF (@Param1 > 0) BEGIN RETURN 100/@Param1; PRINT 'This statement will not be executed'; END IF (@Param1 < 0) BEGIN RAISERROR ('Raise error instead of using RETURN statement.', 2,1); PRINT 'This statement will not be executed'; ENDENDAnalysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0144 : The code following the RAISERROR statement will never be executed. | 9 | 2 |
| 2 | SA0144 : The code following the RETURN statement will never be executed. | 18 | 2 |