Skip to content

SA0144 : The code following the RETURN or the RAISERROR statements will never be executed

Dead code in T-SQL scripts, which is never executed, can cause inefficiencies and increase confusion in code maintenance.

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 code
CREATE PROCEDURE [dbo].[checkForDeadCode]
(
@InputValue BIGINT
)
AS
IF (@InputValue = 0)
BEGIN
RETURN 0;
END
ELSE
BEGIN
RETURN @InputValue + 1;
END
RETURN -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.

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 code
CREATE PROCEDURE [dbo].[checkForDeadCode]
(
@InputValue BIGINT
)
AS
IF (@InputValue = 0)
BEGIN
RETURN 0;
END
ELSE
BEGIN
RETURN @InputValue + 1;
END
RETURN -1;
PRINT 'This message will never be printed'; -- Dead code example
-- After removing dead code
CREATE PROCEDURE [dbo].[checkForDeadCode]
(
@InputValue BIGINT
)
AS
IF (@InputValue = 0)
BEGIN
RETURN 0;
END
ELSE
BEGIN
RETURN @InputValue + 1;
END
-- Removed unreachable PRINT statement

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

Rule has no parameters.

The rule does not need Analysis Context or SQL Connection.

20 minutes per issue.

Design Rules, Code Smells

RETURN (Transact-SQL)

RAISERROR (Transact-SQL)

TRY…CATCH (Transact-SQL)

ALTER PROCEDURE [dbo].[test_DeadCode_Rule]
(
@Param1 INT = 0
)
AS
BEGIN
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';
END
END
  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

Analysis Rules