SA0244 : Database object created,altered or dropped without specifiying schema name
Introduction
Section titled “Introduction”Fully qualifying object names with their schema in CREATE , ALTER , and DROP statements helps avoid confusion with the user’s default schema, as well as errors and security issues.
Description
Section titled “Description”In SQL Server, it is crucial to fully qualify object names in CREATE , ALTER , and DROP statements with their schema. Omitting the schema can result in SQL Server assuming the user’s default schema, which can lead to confusion, errors, or security issues.
For example:
-- Example of problematic queryCREATE TABLE TableName (ID INT);The above statement might unintentionally create the table in the user’s default schema rather than the intended one, potentially leading to access issues or application errors.
-
Lack of clarity: Developers and administrators may not be able to easily discern the schema in which the object resides, leading to maintenance challenges.
-
Security risks: Objects may be unintentionally created in or altered within the wrong schema, making schema-based security measures ineffective.
How to fix
Section titled “How to fix”Ensure all object names in CREATE , ALTER , and DROP statements are fully qualified with their schema to prevent errors and unintended behavior.
Follow these steps to address the issue:
1.Identify all CREATE , ALTER , and DROP statements in your T-SQL scripts that are missing schema qualification.
2.Modify each statement to include the schema name before the object name. For instance, change CREATE TABLE TableName to CREATE TABLE dbo.TableName if the table belongs to the dbo schema.
3.Test the updated scripts by executing them in a safe environment to ensure that objects are being created, altered, or dropped in the correct schema.
For example:
-- Example of corrected queryCREATE TABLE dbo.TableName (ID INT);The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”5 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
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,)
CREATE TABLE Greeting(GreetingId INT IDENTITY (1,1) PRIMARY KEY,Message nvarchar(255) NOT NULL,)DROP TABLE greeting;
DROP TABLE #tempTable1;Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0244 : The table is created without specifiying schema name. | 7 | 13 |
| 2 | SA0244 : The table is dropped without specifiying schema name. | 12 | 11 |