SA0229 : This syntax of RAISERROR is discontinued. Rewrite the statement using the current RAISERROR(…) syntax or consider using THROW

The use of deprecated RAISERROR syntax can lead to compatibility issues in newer versions of SQL Server.

Description

The deprecated RAISERROR syntax refers to using RAISERROR integer ‘string’, which was deprecated starting in SQL Server 2012 and removed in SQL Server 2014. Developers writing or maintaining T-SQL code must be aware of this to ensure compatibility with current SQL Server versions.

For example:

SQL
1-- Deprecated RAISERROR syntax
2RAISERROR 50001 'An error occurred';

This example shows the usage of the deprecated format, which is not supported starting with SQL Server 2014. Using this syntax can result in errors during execution and maintenance challenges as the database progresses to newer SQL Server versions.

  • This deprecated syntax may cause runtime errors in SQL Server 2014 and later versions.

  • It requires code refactoring to utilize supported syntax, increasing maintenance overhead.

How to fix

This guidance helps you update T-SQL code by replacing the deprecated RAISERROR syntax with the current RAISERROR or THROW statements to ensure compatibility with newer SQL Server versions.

Follow these steps to address the issue:

  1. Identify occurrences of the deprecated syntax in your code, such as RAISERROR 50001 ‘An error occurred’;.

  2. Replace the deprecated syntax with the supported RAISERROR syntax: RAISERROR (message_string, severity, state); Ensure you provide appropriate values for message_string, severity, and state.

  3. Alternatively, consider using the THROW statement for simplicity and modernity: THROW 50001, ‘An error occurred’, 1; Adjust the error number, message, and state as needed for your application.

  4. Test the updated code to ensure proper error handling and compatibility with SQL Server 2014 and later versions.

For example:

SQL
1-- Updated statement using RAISERROR
2RAISERROR ('An error occurred', 16, 1);
3
4-- or using THROW
5THROW 50001, 'An error occurred', 1;

Scope

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

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
5 minutes per issue.
Categories

Deprecated Features, Bugs

Additional Information
Example Test SQL
SQL
 1declare @errno int
 2declare @errmsg varchar(500)
 3select @errno=@@error
 4
 5if @errno<>0
 6begin
 7    select @errmsg='Error: '+ convert(varchar(10),@errno)
 8
 9    raiserror 200016 @errmsg
10
11    raiserror(@errmsg,200016,1)
12
13    return 1
14end

Analysis Results
  Message Line Column
1 SA0229 : This syntax of RAISERROR is discontinued. Rewrite the statement using the current RAISERROR(…) syntax or consider using THROW. 9 4
See Also

Other Resources