SA0160 : Deprecated use of @, @@, or names that begin with @@ as Transact-SQL identifiers

Identifiers that start with @ or @@ in T-SQL can lead to confusion and errors in code interpretation.

Description

In T-SQL code, @ is commonly used for declaring and referencing local variables, while @@ is used for system functions (or global variables like @@SERVERNAME). Misusing these conventions by naming tables, columns, or user-defined variables in a similar manner can cause conflicts or unexpected behavior, as these symbols have predefined meanings.

For example:

SQL
1-- Example of problematic use of identifiers
2SELECT @@someGlobalVar FROM MyTable; 
3CREATE TABLE @myTable (ID INT);

In the first example, using @@ suggests a system function, which can be misleading if not correctly defined. In the second example, using @myTable can cause confusion, as it mimics local variable syntax, leading to potential misinterpretation within scripts or stored procedures.

  • Creates ambiguity in distinguishing between system functions and user-defined identifiers.

  • Increases risk of syntax errors or logical errors in queries and stored procedures.

  • Can lead to maintenance challenges and lower code readability.

How to fix

Avoid using identifiers that start with @ or @@ to prevent confusion and errors in T-SQL code interpretation.

Follow these steps to address the issue:

  1. Review the reported identifier and understand its current usage within your database scripts or code.

  2. Rename the identifier to a more descriptive and unique name that does not begin with @ or @@. This will ensure it is not mistaken for a local or system-defined variable.

  3. Update all references to the renamed identifier within your database objects, such as tables, stored procedures, and functions, to ensure consistency and prevent errors.

For example:

SQL
1-- Example of corrected query
2CREATE TABLE MyTable (ID INT);
3SELECT someGlobalVar FROM MyTable;

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
3 minutes per issue.
Categories

Design Rules, Naming Rules, Deprecated Features, Bugs

Additional Information
Example Test SQL
SQL
1declare @ as int, @@ as int, @@a bit ,@@b int

Analysis Results
  Message Line Column
1 SA0160 : Deprecated use of @, @@, or names that begin with @@ as Transact-SQL identifiers. 1 8
2 SA0160 : Deprecated use of @, @@, or names that begin with @@ as Transact-SQL identifiers. 1 18
See Also

Other Resources