SA0073B : Check all User-Defined Types for following specified naming convention

Inconsistent or unclear user-defined types naming can lead to confusion and maintenance challenges.

Description

In T-SQL scripts, user-defined types are created using the CREATE TYPE statement. A major problem arises when the naming conventions for these types are inconsistent or unclear, which can lead to confusion, increased error rates, and difficulties in database management.

For example:

SQL
1-- Poor naming practice for user-defined type
2CREATE TYPE abc123 AS TABLE (...);

This example uses a non-descriptive name that does not convey the type’s purpose or content, complicating schema reviews and future maintenance efforts.

  • Unclear names make it difficult for developers and administrators to understand the database’s schema, especially in large or complex systems.

  • Inconsistent naming can lead to increased cognitive load and errors during development, as team members may misinterpret or misuse types.

`

How to fix

Ensure consistent and meaningful naming for user-defined types in SQL to improve maintainability and clarity.

Follow these steps to address the issue:

  1. Review the existing object name in the CREATE TYPE statement.

  2. Refer to your organization’s naming conventions for database objects to identify the appropriate format and structure for the type name.

  3. Rename the type using a descriptive and consistent name that clearly indicates its purpose and content.

  4. Update any database scripts or application code that reference the renamed type to ensure consistency in the database schema.

For example:

SQL
 1-- Original poorly named user-defined type
 2CREATE TYPE abc123 AS TABLE (
 3    Column1 INT,
 4    Column2 VARCHAR(50)
 5);
 6
 7-- Corrected with a meaningful name following naming conventions
 8CREATE TYPE OrderItemTableType AS TABLE (
 9    ItemID INT,
10    ItemDescription VARCHAR(50)
11);

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters
Name Description Default Value
NamePattern

Deault constraint name pattern.

regexp:[A-Z][A-Za-z]+

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
8 minutes per issue.
Categories

Naming Rules, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
1CREATE TYPE SSN  
2FROM varchar(11) NOT NULL ;  
3
4CREATE TYPE Utf8String   
5EXTERNAL NAME utf8string.[Microsoft.Samples.SqlServer.utf8string] ;  
6
7CREATE TYPE LocationTableType AS TABLE   
8    ( LocationName VARCHAR(50)  
9    , CostRate INT );

Analysis Results
  Message Line Column
1 SA0073B : The user defined type name Utf8String does not match the naming convention. The expected name is [[A-Z][A-Za-z]+]. 4 12
See Also

Other Resources