SA0042A : Avoid using special characters in object names
Introduction
Section titled “Introduction”Using special characters in database object names can lead to issues with object referencing and code readability.
Description
Section titled “Description”Database object names that include special characters-like spaces, brackets, or quotes-can present several challenges. These special characters cause complications in both referencing these objects in queries and maintaining clear, readable code.
For example:
-- Example of problematic namingCREATE TABLE [Employee Data] ( ID INT PRIMARY KEY, Name NVARCHAR(100));SELECT * FROM [Employee Data];This example shows a table name containing a space, which requires the use of brackets for proper referencing. Using special characters like these often leads to cumbersome syntax and increased likelihood of errors.
-
The necessity for bracketed references increases the complexity and reduces the readability of queries.
-
Risk of syntax errors increases when the special characters are not handled correctly.
How to fix
Section titled “How to fix”To resolve issues related to special characters in database object names, rename the problematic objects to eliminate those characters, enhancing both code readability and maintenance.
Follow these steps to address the issue:
1.Identify the database objects with special characters. Look for names that include spaces, brackets, quotes, or any other non-standard characters.
2.Decide on a new name for the object that adheres to standard naming conventions. Ensure the new name is descriptive yet concise and does not include special characters.
3.Use the sp_rename stored procedure in T-SQL to rename the object. Syntax: sp_rename 'old_name', 'new_name' .
4.Update any T-SQL scripts or application code that reference the old object name to use the new name.
5.Verify that the object references have been successfully updated and that all functionality is intact.
For example:
-- Rename table with special characters in its namesp_rename '[Employee Data]', 'EmployeeData';-- Update queries to use the new table nameSELECT * FROM EmployeeData;The rule has a ContextOnly scope and is applied only on current server and database schema.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule requires Analysis Context. If context is missing, the rule will be skipped during analysis.
Effort To Fix
Section titled “Effort To Fix”20 minutes per issue.
Categories
Section titled “Categories”Naming Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.