Code Analysis


There should be no USE statement in batch

It is unusual to need to jump between database contexts in the middle of a batch, though it is allowed.

Available in

SQL Code Guard

(warning) As of version, this rule is no longer available in SQL Prompt.

The USE command can only accept a literal database name. It cannot use a synonym, variable or expression. It is intended only as the way of switching the user context.It requires that a database name should be embedded somewhere in the body of the code.

For reliable installation, this requires a SQLCMD macro or similar to allow the installer to specify the name when necessary.

The use of USE itself isn't wrong, though it causes an error in Azure SQL Database. It is, in fact, often required in Admin scripts and DDL. If, however, you need to use it in application code, there is probably something wrong with the design of the database(s), since batches should always for security, run in the database context of that database user. Any cross-database work should be done with synonyms, views or iTVFs.

Didn't find what you were looking for?