SA0273 : The USE statement must be the first statement in the script

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

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:

SQL
1-- Problematic script with misplaced USE statement
2CREATE TABLE TestTable (ID INT);
3USE OtherDatabase;
4ALTER 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

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:

SQL
1-- Proper placement of the USE statement
2USE OtherDatabase;
3CREATE TABLE TestTable (ID INT);
4ALTER TABLE TestTable ADD Column1 INT;

Scope

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

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
2 minutes per issue.
Categories

Design Rules, New Rules

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE TABLE Test.Greeting
 2(
 3GreetingId INT IDENTITY (1,1) PRIMARY KEY,
 4Message nvarchar(255) NOT NULL,
 5)
 6
 7
 8USE DB1
 9
10
11INSERT INTO Test.Greeting (Message) 
12SELECT 'Hello!'
13UNION ALL 
14SELECT 'Hi!'
15UNION ALL
16SELECT 'Hello, world!'
17
18INSERT INTO Test.Greeting (Message) 
19VALUES ('How do yo do?'),
20        ('Good morning!'),
21        ('Good night!')
22
23DELETE  Test.Greeting WHERE GreetingId = 3
24
25SELECT * FROM Test.Greeting g 
26WHERE 
27g.Message like 'Hello%'
28
29DROP TABLE Test.Greeting

Analysis Results
  Message Line Column
1 SA0273 : The USE statement must be the first statement in the script. 10 0
See Also

Other Resources