Skip to content

SA0042A : Avoid using special characters in object names

Using special characters in database object names can lead to issues with object referencing and code readability.

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 naming
CREATE 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.

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 name
sp_rename '[Employee Data]', 'EmployeeData';
-- Update queries to use the new table name
SELECT * FROM EmployeeData;

The rule has a ContextOnly scope and is applied only on current server and database schema.

Rule has no parameters.

The rule requires Analysis Context. If context is missing, the rule will be skipped during analysis.

20 minutes per issue.

Naming Rules, Code Smells

There is no additional info for this rule.

Analysis Rules