Microsoft KB Archive/307864

= Error message when you connect to an instance of SQL Server: &quot;Cannot open user default database&quot; =

Article ID: 307864

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft SQL Server 2005 Standard Edition
 * Microsoft SQL Server 2005 Enterprise Edition
 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Workgroup Edition
 * Microsoft SQL Server 2005 Express Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition

-



This article was previously published under Q307864



SYMPTOMS
Each user has a default database. When you connect to computer that is running Microsoft SQL Server, and you do not specify a login database, the default database is used. However, if the default database is unavailable at the time of the connection, you may not be able to connect. Instead, you receive error message 4062 or error message 4064. The text of the error messages is as follows:

Cannot open user default database



CAUSE
The user default database is unavailable at the time of connection. It is possible that the database:
 * Is in suspect mode.
 * No longer exists.
 * Is in single user mode and the only available connection is already being used by someone else or by something else.
 * Has been detached.
 * Has been set to the RESTRICTED_USER state.
 * Is offline.
 * Is set to emergency status.
 * Does not have the login account mapped to a user or the user has been denied access.
 * Is part of a database mirror.

Additionally, the login account may be a member of multiple groups and the default database for one of those groups is unavailable at the time of connection.



WORKAROUND
To work around this behavior, specify a valid, available database in the connection string. To avoid the error when the user's default database is unavailable, log on as a user who can modify logins. Then, change the user's default database to a database that is currently available for a connection.

SQL Server 2005
You can use the sqlcmd utility to change the default database in SQL Server 2005. To do this, follow these steps:  Click Start, click Run, type cmd, and then press ENTER. Use one of the following methods, depending on the kind of authentication that the SQL Server login uses:  If the SQL Server login uses Microsoft Windows authentication to connect to the instance, type the following at the command prompt, and then press ENTER:

sqlcmd –E -S  –d master

 If the SQL Server login uses SQL Server authentication to connect to the instance, type the following at the command prompt, and then press ENTER:

sqlcmd -S  -d master -U   -P



Note  is a placeholder for the name of the SQL Server 2005 instance to which you are connecting. is a placeholder for the SQL Server login whose default database has been dropped. is a placeholder for the SQL Server login password. At the sqlcmd prompt, type the following, and then press ENTER:

ALTER LOGIN  WITH DEFAULT_DATABASE =

Note  is a placeholder for the name of the existing database that can be accessed by the SQL Server login in the instance.</li> At the sqlcmd prompt, type GO, and then press ENTER.</li></ol>

SQL Server 2000 and SQL Server 7.0
You can use the osql utility to change the default database in SQL Server 2000 and in SQL Server 7.0. To do this, follow these steps: <ol> At a command prompt, type the following and then press ENTER:

C:\>osql -E

</li> At the osql prompt, type the following and then press ENTER:

1>sp_defaultdb 'user's_login', 'master'

</li> At the second prompt, type the following and then press ENTER:

2>go

</li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

MORE INFORMATION
To demonstrate this behavior, try to connect with OSQL and use the -d option with an existing database that is in a normal online status. The connection succeeds without any error message.

Conversely, you receive responses that are similar to the following from various SQL Server client utilities when the database is unavailable:  SQL Server Management Studio (SQL Server 2005)

Cannot connect to

ADDITIONAL INFORMATION:

Cannot open user default database. Login failed. Login failed for user ' '. (Microsoft SQL Server, Error: 4064)

</li> Using Sqlcmd utility in SQL Server 2005

Msg 4064, Level 11, State 1, Server, Line 1

Cannot open user default database. Login failed.

Msg 18456, Level 14, State 1, Server, Line 1 Login failed for user ' '.

</li> SQL Server 2000 Query Analyzer (QA)

Unable to connect to server TESTSERVER:

Server: Msg 4064, Level 16, State 1

[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open user default database. Login failed.

</li> SQL Server 2000 Server Enterprise Manager (SEM)

A connection could not be established to TESTSERVER - Cannot open user default database 'dbid'. Using master database instead.

Please verify SQL Server is running and check your SQL Server registration properties (by right-clicking on the TESTSERVER node) and try again.

</li> A command prompt using SQL Server 2000 OSQL

You receive the following error message when you use the C:\>osql -E command:

Cannot open user default database. Login failed.

</li> SQL Server 7.0 Query Analyzer (QA)

Unable to connect to server \\TESTSERVER:

Server: Msg 4062, Level 16, State 1

[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open user default database '<ID>'. Using master database instead.

</li> A command prompt using SQL Server 7.0 OSQL

You receive the following error message when you use the C:\>osql -E command:

Cannot open user default database 'dbid'.

Using master database instead.

</li></ul>

Keywords: kbprb KB307864

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.