SA0273 : The USE statement must be the first statement in the script
Introduction
Section titled “Introduction”The improper placement of the USE statement in a T-SQL script can lead to confusion, errors, and unintended behavior when setting the database context.
Description
Section titled “Description”The USE statement sets the current database context for executing SQL commands. When placed anywhere other than the first line of a script, it may cause scripts to behave unpredictably by executing some commands on the wrong database before setting the intended context.
For example:
-- Problematic script with misplaced USE statementCREATE TABLE TestTable (ID INT);USE OtherDatabase;ALTER TABLE TestTable ADD Column1 INT;In this script, the table is created before the USE statement sets the database context to OtherDatabase . If the default or initial database context differs, commands could affect the wrong database, causing unintended changes or errors.
-
Poor placement may lead to operations being executed on the wrong database, affecting data integrity and causing unexpected results.
-
Scripts are harder to share, automate, or reuse reliably in different environments, such as CI/CD pipelines, due to unpredictable behavior.
Place the USE statement as the first line in scripts to consistently set the database context, enhancing clarity and preventing execution errors.
How to fix
Section titled “How to fix”To ensure the intended database context is set correctly, the USE statement should be placed at the beginning of SQL scripts. This prevents confusion and errors arising from commands being executed in an incorrect database context.
Follow these steps to address the issue:
1.Place the USE statement as the first line of your T-SQL script. This ensures that all subsequent SQL commands are executed within the correct database context.
2.Review the script for any instances where database-specific operations precede the USE statement. Move these operations if necessary to ensure they occur after setting the database context.
3.Test the script in a controlled environment to verify that the operations affect the correct database after adjusting the USE statement’s position.
For example:
-- Proper placement of the USE statementUSE OtherDatabase;CREATE TABLE TestTable (ID INT);ALTER TABLE TestTable ADD Column1 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”2 minutes per issue.
Categories
Section titled “Categories”Design 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,)
USE DB1
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 | SA0273 : The USE statement must be the first statement in the script. | 10 | 0 |