SA0140 : Reserved keyword is not in the required case
Introduction
Section titled “Introduction”Inconsistent casing of reserved keywords in T-SQL can reduce code readability and maintainability.
Description
Section titled “Description”In T-SQL code, using consistent casing for reserved keywords enhances code readability and aligns with best practices for writing clear and maintainable SQL scripts.
For example:
-- Example of inconsistent casingSelect * from Employees where employee_id = 1;In the example above, the variations in keyword casing can make the SQL code harder to read and may lead to confusion during maintenance or code reviews. Following a consistent casing style can help in standardizing codebases, especially in collaborative environments.
-
Inconsistent casing may lead to misunderstandings among team members about coding standards.
-
It can contribute to difficulties in maintaining or refactoring code over time.
-
Standardizing keyword casing improves the overall readability of SQL scripts, facilitating better communication among developers.
How to fix
Section titled “How to fix”Ensure that reserved keywords in your T-SQL code are consistently cased to improve readability and maintainability.
Follow these steps to address the issue:
1.Review your T-SQL code to identify any reserved keywords with inconsistent casing. Reserved keywords include terms like SELECT , FROM , and WHERE .
2.Determine the casing convention you want to apply, such as all uppercase or all lowercase, to maintain consistency across your codebase.
3.Update the reserved keywords in your script to conform to the chosen casing style. You can manually change the keyword case or use a tool or script to automate the process.
For example:
-- Example of corrected query with consistent casingSELECT * FROM Employees WHERE employee_id = 1;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”| Name | Description | Default Value |
|---|---|---|
| RequiredCase | The parameter specifies the required case of the reserved keywords. | UPPERCASE |
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”2 minutes per issue.
Categories
Section titled “Categories”Naming Rules
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”CREATE TABLE Test.Greeting(GreetingId INT identity (1,1) PRIMARY KEY,Message nvarchar(255) NOT NULL,)
INSERT INTO Test.Greeting (Message)SELECT 'Hello!'UNION ALLSELECT 'Hi!'UNION ALLSELECT 'Hello, world!'
insert into Test.Greeting (Message)values ('How do yo do?'), ('Good morning!'), ('Good night!')
DELETE Test.Greeting wherE GreetingId = 3
SELECT * FROM Test.Greeting gwhereg.Message like 'Hello%'
drop TABLE Test.GreetingExample Test SQL with Automatic Fix
Section titled “Example Test SQL with Automatic Fix”CREATE TABLE Test.Greeting(GreetingId INT IDENTITY (1,1) PRIMARY KEY,Message nvarchar(255) NOT NULL,)
INSERT INTO Test.Greeting (Message)SELECT 'Hello!'UNION ALLSELECT 'Hi!'UNION ALLSELECT 'Hello, world!'
INSERT INTO Test.Greeting (Message)VALUES ('How do yo do?'), ('Good morning!'), ('Good night!')
DELETE Test.Greeting WHERE GreetingId = 3
SELECT * FROM Test.Greeting gWHEREg.Message LIKE 'Hello%'
DROP TABLE Test.GreetingAnalysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0140 : Reserved keyword is not in the required case. | 3 | 15 |
| 2 | SA0140 : Reserved keyword is not in the required case. | 14 | 0 |
| 3 | SA0140 : Reserved keyword is not in the required case. | 14 | 7 |
| 4 | SA0140 : Reserved keyword is not in the required case. | 15 | 0 |
| 5 | SA0140 : Reserved keyword is not in the required case. | 19 | 22 |
| 6 | SA0140 : Reserved keyword is not in the required case. | 22 | 0 |
| 7 | SA0140 : Reserved keyword is not in the required case. | 23 | 10 |
| 8 | SA0140 : Reserved keyword is not in the required case. | 25 | 0 |