Edit

USE (Transact-SQL)

Applies to: SQL Server Azure SQL Managed Instance Warehouse in Microsoft Fabric

Changes the database context to the specified database or database snapshot.

Transact-SQL syntax conventions

Syntax

USE { database_name }
[ ; ]

Arguments

database_name

The name of the database or database snapshot to which the user context is switched. Database and database snapshot names must comply with the rules for identifiers.

In Azure SQL Database and SQL database in Microsoft Fabric, the USE statement isn't available. Instead, connect directly to the desired database. In Azure SQL Database, the database parameter can only refer to the current database. If a database other than the current database is provided, the USE statement doesn't switch between databases, and error code 40508 is returned.

Remarks

When a SQL Server login connects to SQL Server, the login is automatically connected to its default database and acquires the security context of a database user. If no database user is created for the SQL Server login, the login connects as guest. If the database user doesn't have CONNECT permission on the database, the USE statement fails. If no default database is assigned to the login, its default database is set to master.

USE is executed at both compile and execution time and takes effect immediately. Therefore, statements that appear in a batch after the USE statement are executed in the specified database.

Permissions

Requires CONNECT permission on the target database.

Examples

The following example changes the database context to the AdventureWorks2025 database.

USE AdventureWorks2022;
GO