This post is intended to help explain the USE command in SQL Server. In short, the USE command allows you to change the context or scope of the database you are currently working on for a SQL query.
When working with and querying SQL Server, it is often necessary to switch between different databases or perform actions on specific databases. The USE command in SQL Server allows you to do just that by changing the context or scope of the database you are currently working on.
It’s important to note that the USE command does not work with Azure SQL Database. In case you don’t know the names of the databases and you are unable to see them in the Management Studio (SSMS) Object Explorer, you can query sys.databases to see the available databases.
Example of USE in MSSQL
If you’re working with SQL Server you’ll see the USE command everywhere. It’s straightforward to use… ha!
USE <database-name>; GO
The semi-colon goes at the end of the database name. Do not include <> characters, but you may open and close the database name with [square-brackets].
When we open a new query in SQL Management Studio (SSMS), your database context will automatically be set to the default database when you open a new query. We’re changing it in this demo, but you can also query [database].[schema].[table] instead of switching the database you are querying like this.
If you need to change the default database for your SQL queries you can check out how this is done in my other blog post – How to Change Default Database for a SQL User. Otherwise, hope this guide was useful!