Skip to content

SA0228 : WITHIN is now a reserved keyword. References to objects or columns named 'within' will fail

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.

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 query
SELECT 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.

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 identifier
SELECT [WITHIN] FROM TableName;

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.

3 minutes per issue.

Deprecated Features, Bugs

Deprecated Database Engine Features in SQL Server 2017

select * from within
select within from [within]
  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

Analysis Rules