SA0228 : WITHIN is now a reserved keyword. References to objects or columns named 'within' will fail
Introduction
Section titled “Introduction”Using the reserved keyword WITHIN as an identifier (e.g., for table or column names) in SQL Server can cause errors, as it is now a reserved keyword.
Description
Section titled “Description”In SQL Server, using reserved keywords as identifiers can lead to errors in your T-SQL code. The keyword WITHIN is one such reserved word. If you have database objects like tables, columns, or other identifiers named ‘within’, they will cause problems because WITHIN is now a reserved keyword.
For example:
-- Example of problematic querySELECT within FROM TableName;In this example, within is used as a column name, which will result in an error because WITHIN is a reserved keyword. To avoid these issues, you must rename the column or use delimited identifiers by wrapping it in double quotes or square brackets.
-
Queries will fail to compile, leading to runtime errors and system failures.
-
It can make the codebase inconsistent and difficult to maintain if not resolved promptly.
How to fix
Section titled “How to fix”This section provides guidance on how to fix issues caused by using reserved keywords as identifiers in SQL Server.
Follow these steps to address the issue:
1.Identify instances where reserved keywords like WITHIN are used as identifiers, such as column or table names, in your T-SQL code.
2.If renaming is not possible, delimit the identifier using square brackets. Replace within with [WITHIN] in your query to avoid conflicts with reserved keywords.
3.Review and update any related part of your codebase to ensure consistency, especially when identifiers are referenced in multiple queries or scripts.
For example:
-- Example of corrected query using delimited identifierSELECT [WITHIN] FROM TableName;The 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”3 minutes per issue.
Categories
Section titled “Categories”Deprecated Features, Bugs
Additional Information
Section titled “Additional Information”Deprecated Database Engine Features in SQL Server 2017
Example Test SQL
Section titled “Example Test SQL”select * from within
select within from [within]Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0228 : WITHIN is now a reserved keyword. References to objects or columns named ‘within’ will fail. | 1 | 14 |
| 2 | SA0228 : WITHIN is now a reserved keyword. References to objects or columns named ‘within’ will fail. | 3 | 7 |