Sql error 18456 severity 14 state 38

Had the following issue:
  • Remove From My Forums
  • Question

  • Had the following issue:

    Login failed for user ‘sa’. Reason: Failed to open the explicitly specified database. Error: 18456, Severity: 14, State: 38

    SQL 2008 R2. 

    This issue happened randomly and on specific timing. 

    Any specific reason or issue? 

Answers

    • Proposed as answer by

      Tuesday, July 7, 2015 6:08 AM

    • Marked as answer by
      Lydia ZhangMicrosoft contingent staff
      Tuesday, July 14, 2015 6:47 AM
    • Proposed as answer by
      Lydia ZhangMicrosoft contingent staff
      Tuesday, July 7, 2015 6:09 AM
    • Marked as answer by
      Lydia ZhangMicrosoft contingent staff
      Tuesday, July 14, 2015 6:47 AM
  • Had the following issue:

    Login failed for user ‘sa’. Reason: Failed to open the explicitly specified database. Error: 18456, Severity: 14, State: 38

    SQL 2008 R2. 

    This issue happened randomly and on specific timing. 

    Any specific reason or issue? 

    The default database to which SA want to connect is no longer available or still not online. Does this issue com when database is recovering or trying to com online after reboot if that is case its normal. BTW are you using SA to connect your application
    ?

    You can see what default database is set from below. Just for sake make default database as master

    If you are using SQL express make sure AUTO CLOSE is set to false for database.Further troubleshooting can be done from below article

    http://social.technet.microsoft.com/wiki/contents/articles/2102.how-to-troubleshoot-connecting-to-the-sql-server-database-engine.aspx


    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it

    My Technet Wiki Article



    MVP

    • Proposed as answer by
      Lydia ZhangMicrosoft contingent staff
      Tuesday, July 7, 2015 6:09 AM
    • Marked as answer by
      Lydia ZhangMicrosoft contingent staff
      Tuesday, July 14, 2015 6:47 AM

  • Remove From My Forums
  • Question

  • Hi,

    I’ve just moved all my SQL Server 2005 databases to a SQL Server 2008 R2 server within the same domain. I am 100% confident that all databases are present and accounted for. I used sp_help_revlogin to get the logins from 2005 server to the 2008 R2 server. All
    logins were created successfully. I then ran the following:

    ALTER USER [MYDOMAINMYACCOUNT] WITH LOGIN = [MYDOMAINMYACCOUNT]

    The problem is, I am getting the following error in the SQL Server Error log:

    Login failed for user 'MYDOMAINMYACCOUNT'. Reason: Failed to open the explicitly specified database [CLIENT:XX.XX.XX.XXX]
    
    Error: 18456, Severity: 14, State: 38
    

    I get this error even if I give the give the domain account sysadmin privileges on this box (which it doesn’t have normally).

    I query sys.dm_os_ring_buffers and I can see several RING_BUFFER_SECURITY_ERROR errors, with ErrorCode: 0x534, APIName: LookupAccountSID. This, as far as I know, is a problem resolving the login details against AD. Then why does the very same account work perfectly
    fine on the same server in all the databases that it’s been permissioned for?

    The new 2008 server is a clone from a template created in vmware.  All of the services were already set to start with the domain account.  I don’t know if this has a
    part to play with the problem.

    Any help greatly appreciated.

    Andrew


    Thanks, Andrew

Answers

  • the logins were created from scratch within the domain, using a script created from sp_help_revlogins on the 2005 server.  They weren’t cloned as part of the VM, as I knew that the login/AD SIDs would get messed up.

    Although the account is in windows groups that are used by other users, the permissions in SQL Server are unique to that account.  It’s various role membership/object permissioning, etc. means that I wouldn’t be able to change the software to use another
    account in the same AD group.


    Thanks, Andrew

    • Marked as answer by

      Wednesday, October 24, 2012 1:54 PM

WRITE FOR US

SQL Server DBA & Architect, EE Solution Guide

I’m a SQL Server Architect experienced in Design, Development, Administration, Performance Tuning.. Exploring BI, SSIS & Data Science Arena!

Published: 2021-01-28

Browse All Articles > How to Fix SQL Server Login Failed Error 18456 State 38

How to Fix Microsoft SQL Server Error 18456? – TheITBros

One of the most common issues faced while connecting to a SQL Server instance is «Login failed for user ‘username’. (Microsoft SQL Server, Error: 18456)». In addition to the Error No. SQL Server will provide an Error State no. which will be helpful to identify in more detail about the Login Authentication failure. Majority of the Authentication related errors might arise due to incorrect username or password, disabled SQL Authentication mode, expired password, deleted user account or non-existent database.

Please find below the various Error State no. for Error no. 18456 from MSDN below.

In this article, we’ll take a closer look at State no. 38 for error 18456 while trying to authenticate on Microsoft SQL Server and address how to fix it. As shown in the Error State above, we can receive Error State 38 whenever the database requested wasn’t available in that instance and hence can happen for even SA or any domain administrator account. For example, if you run Microsoft SQL Server Management Studio and try to login to the SQL server with a domain administrator account, the following error may appear:

TITLE: Connect to Server

——————————

Cannot connect to MySQLServerSQLInstance.

——————————

ADDITIONAL INFORMATION:

2020-12-15 12:19:34.56 Logon      Error: 18456, Severity: 14, State: 38.

2020-12-15 12:19:34.56 Logon      Login failed for user ‘<SQLDomain><Admin’. Reason: Failed to open the explicitly specified database. [CLIENT: XXX.XX.XX.XXX]

Fixing the error 18456, Severity: 14, State:38

As explained above, error state number 38 clearly mentions that the Specified database does not exist or inaccessible. Please be noted that the Error State no and Description can vary across SQL Servers like States 38 and 40 (in SQL 2008 and later) and States 16 and 27 (before version 2008) to indicate that the specified database does not exist. Few possible reasons include the database could be offline, shutdown, deleted, dropped, renamed, auto-closed, or inaccessible for some other reason like corruption.

Now to fix this error, we have to attempt to log into the SQL Server to understand what is going on with the specified database. You will notice in the error that the name of the database is not specified. What if you do not know the database the log on user is attempting to connect to? In such scenarios, in SQL Server Management Studio (SSMS) connect to that instance using an Admin account and in object explorer expand databases to see if that database exists and what state is in.



We can also achieve the same using T-SQL by opening a new query window and running the below code and check the State_Desc column as explained in my previous article SQL Server Database Health Checks(link in reference section)

SELECT * from sys.databases

Depending on the results, we can identify how we need to fix the problem.

For example

1. If the database was renamed, then connect to the correct database.

2. If the database doesn’t exist, then check with your DBA to find out why your database was missing.

3. If the database is OFFLINE, then check with your DBA to bring the database ONLINE.

4. If the database is corrupted and couldn’t start properly, then we can encounter the below issue as well. In order to fix the corruption issues, we need to run DBCC CHECKDB to identify what levels of database corruption was there. Command used to perform DBCC CHECKDB provided below for reference and precautionary steps to be taken while executing DBCC CHECKDB was explained in my previous article Estimate Execution time of DBCC CHECKDB(link provided in reference).

DBCC CHECKDB

Executing DBCC CHECKDB provided the below results indicating clearly that there was a database corruption.

To recover from the corruption, we can try recovering the database from recent backups if available or else execute DBCC CHECKDB WITH REPAIR_ALLOW_DATA_LOSS option.

DBCC CHECKDB(MyTestDB, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;

Execution of DBCC CHECKDB completed successfully resolving the corruption issues and showed no errors bringing the database back online allowing users to login successfully.


Conclusion

As you would have noticed, understanding the Error Message and State no. provided by SQL Server is critical and once identified properly, we can resolve a complex Database corruption issue successfully. As explained in my article about DBCC CHECKDB command, we should be more careful while executing it as it has potential risks to lose data in case of complex corruptions. What will happen if DBCC CHECKDB execution wasn’t successful or the server doesn’t have enough resources to execute DBCC CHECKDB? If you ever encounter those scenarios, then try to restore from the scheduled Full and Transactional Log Backups (which is a must responsibility for every DBA).

If by some chances, you don’t have a Scheduled Backup in place, then I would like to introduce to you a robust
SQL Database recovery tool that will not only fix your database with the least minimal data loss and potentially help you restore deleted records from the database. Click on the product to learn more about the new and improved
Stellar SQL Database Recovery Tool.

References:

1. MSDN Article to show various possible Error States —
https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-18456-database-engine-error?view=sql-server-ver15

2. SQL Server Database Health Checks —
https://www.experts-exchange.com/articles/35491/SQL-Server-Database-Health-Checks.html

3. Estimate Execution time of DBCC CHECKDB —
https://www.experts-exchange.com/articles/35174/Estimate-Execution-time-of-DBCC-CHECKDB.html

I’m a SQL Server Architect experienced in Design, Development, Administration, Performance Tuning.. Exploring BI, SSIS & Data Science Arena!

This article will help you fixing SQL Server Error: 18456, Severity: 14, State: 38, Reason: Failed to Open the Explicitly Specified Database. We were getting below error in our SQL Server error log for one of our login.

Login Failed – Error: 18456, Severity: 14, State: 38

Reason: Failed to Open the Explicitly Specified Database

Fix Error “Failed to Open the Explicitly Specified Database”

The error log is saying “Failed to Open the Explicitly Specified Database“, it means either your have specified wrong database in your connection string or your login does not have access to the specified database. You can validate below options to isolate this issue.

  • Check whether you have specified correct database in your connection string or not
  • If you are using correct database then whether the login you are using to establish database connection has access to this specified database or not.
  • Correct if you find something in any of the above two options.
  • Check login properties whether it is using correct database as default database configuration or not. If correct database is defined then login has access to that database or not. I will explain about this step in below section as well.
  • You should also check whether number of connections to the database is exhausted or not. Review your maximum number of connections setting.
  • Ensure your database is not set to AUTO CLOSE setting or not inaccessible. Database should be online and healthy state to accept connections.
  • Make sure you have a trust between both machines, database server and the machine from where you are trying to connect. I would suggest you to read below article as well where i have explained various steps to troubleshoot SQL Server connection issues.

Read more articles on connection issues:

  • How to fix SQL Server error 53 – Not able to open database connection
  • Fix SQL Server error 18456 – Login failed for user

Let’s learn how to check default database settings for a SQL Server login and a user mapping for any database to the login.

Check Login Properties

The solution to fix this issue can also come by reviewing the properties of this login. Launch its properties window and change the default database to the correct one if this setting is showing wrong database. Sometimes, we drop the databases from the SQL Server instances but never update other objects. If this login is not showing any database and it is showing blank then click at its drop-down to choose your desired database as the default database for this login. Click on Ok button to apply that change. You will stop getting this issue after making this change.

You can open login properties windows by right click at the identified login and then choose properties from dropdown options.

Sometimes, you have chosen the default database as “master” but your login doesn’t have connect permission on master database so ensure you have at least connect permission on the database which you are choosing as the default database for your login. You can click at “User Mapping” tab from left side pane of the login properties window. You will get all databases in top side of right side pane and level of access in bottom side pane. Click at your database from right side pane along with its level of permission which you want to assign. Click at Ok button to apply this change.

Have a look at below screenshot to understand default database of a login and from where you can access User Mapping tab to provide appropriate access to your database. Your login should have access to both databases, the one mentioned as default database for your login and the specified database in connection strings.

SQL Server Login properties window

You can fix SQL error “Failed to Open the Explicitly Specified Database” by following above steps. Write your feedback if you have fixed this issue by other way. I will update this article accordingly with credits.

  • Author
  • Recent Posts

Manvendra Deo Singh

I am working as a Technical Architect in one of the top IT consulting firm. I have expertise on all versions of SQL Server since SQL Server 2000. I have lead multiple SQL Server projects like consolidation, upgrades, migrations, HA & DR. I love to share my knowledge. You can contact me on my social accounts for any consulting work.

Manvendra Deo Singh

Понравилась статья? Поделить с друзьями:
  • Sql error 42501 error permission denied for relation
  • Sql error 17009 99999 закрытая команда
  • Sql error 17008
  • Sql error 42000 sqlstate 42000
  • Sql error 17003