Choosing a Database

All objects in Microsoft® SQL Server™ are stored in databases. All references to SQL Server objects have to be resolved to the specific database in which they reside.

To resolve implicit database references, SQL Server uses the concept of a current database. Every connection to SQL Server always has a database set as the current database. All object references that do not specify a database name are assumed to refer to the current database. For example, if a connection has Northwind set as its current database, any statement referring to an object named Products is resolved to the Products table in Northwind.

Every SQL Server login has a default database. At the time the login is defined by a member of the sysadmin fixed server role, the default database for the login can be specified. If a default database is not specified, master becomes the default database for the login. The default database for a login can be changed later using the sp_defaultdb stored procedure.

When you first connect to SQL Server, the default database for the login is usually made the current database. You can, however, specify that a specific database be made the current database at connect time. This request overrides the default database designated for the login. Here are the ways you can specify a database on a connect request:

You can switch the current database setting at any time while you are connected to SQL Server. This is called using, or choosing, a database. Here are ways you can switch the current database:


Note In many Transact-SQL reference examples, “USE pubs” or “USE Northwind” is the first line in the example. This ensures the examples are executed against the explicitly specified database instead of another database.


See Also
osql Utility USE

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.