Menu & Search
USE Command in SQL Server

USE Command in SQL Server

The Use command in SQL Server is a way to change context to a specified database, which I often refer to this as scoping to a database for a query.

When you log into SQL Server using SQL Server Management Studio (SSMS) your database context will be automatically set to your default database which was defined during the creation of your SQL login.

This means if you create a new query (Ctrl + N) in SSMS you’ll automatically be scoped to that database. You can switch context by selecting the drop-down menu in SSMS or by running the USE command:

USE animals;

The USE command does not work with Azure SQL Database.

If you don’t know database names and can’t see them in the Object Explorer, try see if you can query sys.databases (SELECT * FROM sys.databases) to confirm what’s on the SQL instance.

This here is an example of utilising the USE command within a sequency of queries:

That’s it on this one. There’s nothing you need to over-think here!


1 Comment

  1. […] scope to the desired database before running this […]