USE Statement

In SQL Server 6.5, when the USE statement is used in a batch, dynamic execution of the USE statement retrieves a different database name than if the USE statement was not in a batch in What's New for SQL Server 6.5.

Changes the database context.

Syntax

USE database_name

where

database_name
Specifies the database to which the user context will be switched.

Remarks

The execution of a USE statement takes effect immediately. This statement is executed both at compile and execution time, so statements that appear after the USE statement in a batch will be executed in the specified database.

It is likely that you will be connected automatically to the master database when you log in to SQL Server. Unless a default database has been set up for your login ID, you must execute the USE statement to change from master to another database.

You must be a known user in the database to which you're changing. If you are not known in the database, SQL Server displays a message. The owner of the database must give you access by executing the sp_adduser system stored procedure.

Users not recognized by name within the destination database can still be allowed access if a guest user exists in that database. A database owner can add the special guest user to any user database by using the sp_adduser system stored procedure. By default, the guest user is added to the master database during installation.

Permission

Permission is assigned when the database owner assigns permission by executing the sp_adduser system stored procedure.

See Also

CREATE DATABASE sp_addalias
DROP DATABASE sp_adduser
EXECUTE sp_defaultdb