USE (Transact-SQL)
Collapse the table of content
Expand the table of content

USE (Transact-SQL)


THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008)noAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

Changes the database context to the specified database or database snapshot in SQL Server.

Topic link icon Transact-SQL Syntax Conventions

USE { database }  

-- Azure SQL Data Warehouse and Parallel Data Warehouse  
USE database_name   

Is 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, the database parameter can only refer to the current database. The USE statement does not switch between databases, error code 40508 is returned. To change databases, you must directly connect to the database.

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 has been created for the SQL Server login, the login connects as guest. If the database user does not have CONNECT permission on the database, the USE statement will fail. If no default database has been assigned to the login, its default database will be 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.

Requires CONNECT permission on the target database.

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

USE AdventureWorks2012;  

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

USE AccountingDB;  

Principals (Database Engine)
EXECUTE (Transact-SQL)

Community Additions

© 2016 Microsoft