USE

Transact-SQL Reference

Transact-SQL Reference

USE

Changes the database context to the specified database.

Syntax

USE { database }

Arguments

database

Is the name of the database to which the user context is switched. Database names must conform to the rules for identifiers.

Remarks

USE executes 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.

When logging in to Microsoft® SQL Server™, users are usually connected to the master database automatically. Unless a default database has been set up for each user's login ID, each user must execute the USE statement to change from master to another database.

To change context to a different database, a user must have a security account for that database. The database owner provides the security accounts for the database.

Permissions

USE permissions default to those users who are assigned permissions by the dbo and sysadmin fixed server roles executing sp_adduser, or by the sysadmin fixed server role and the db_accessadmin and db_owner fixed database roles executing sp_grantdbaccess. Users without a security account in the destination database can still be allowed access if a guest user exists in that database.

See Also

CREATE DATABASE

DROP DATABASE

EXECUTE

sp_addalias

sp_adduser

sp_defaultdb

Using Identifiers