63

I am using windows 8.1 and SQL Server 2012.

I was using an OS account "Manoj" for accessing SQL SERVER with windows authentication. Recently I have deleted my user account "Manoj" of OS and created a new account with same name "Manoj".

But the system took the new account as "Manoj_2". This change keeps me out from accessing the old databases, I have created.

It says that

The database [dbName] is not accessible. (ObjectExplorer)

whenever I try to access any of the previous DBs I have created.

I used to create new login in SQL Server for "Manoj_2", with default DB as "master". But still the problem persists.

I cannot able to detach the DBs. I am unable to expand the DBs.

Note: In OS, I have admin rights for the "Manoj" account.

Please anybody tell me, what to do? either with OS or with SQL Server

5
  • You have to first create a login on the server-level for your SQL Server instance that uses your new OS account; then you need to create users for that login in each database you want to access
    – marc_s
    Oct 5, 2014 at 7:55
  • Cannot drop the existing login DROPPING THE LOGIN DELETE FAILURE TITLE: Microsoft SQL Server Management Studio ------------------------------ Drop failed for Login 'IRON\Manoj_2'. (Microsoft.SqlServer.Smo) An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) Cannot drop the login 'IRON\Manoj_2', because it does not exist or you do not have permission. (Microsoft SQL Server, Error: 15151) Oct 5, 2014 at 8:07
  • 1
    I didn't say you had to drop anything .....
    – marc_s
    Oct 5, 2014 at 8:08
  • Sorry, as you suggested I tried to create the login on the server-level. But it seems that I have already created the login and still cannot even expand the database in the tree view. So I tried to drop the LOGIN, which is followed by the above error. Oct 5, 2014 at 8:17
  • 1
    So if you have a login IRON\Manoj_2 with a default database master - can you open the Security > Logins node in Object Explorer, find your login, right-click and go Properties and then in the User Mapping section define which databases this login has to have access to?
    – marc_s
    Oct 5, 2014 at 8:49

22 Answers 22

40

For this situation you have to connect to database in Single-User mode.

Starting SQL Server in single-user mode enables any member of the computer's local Administrators group to connect to the instance of SQL Server as a member of the sysadmin fixed server role.

Here you can find step-by-step instruction to do this.

In short you must start the sqlserver instance with parameters -m, after start Sql Server Management Studio with windows authentication.

Now you are a sysadmin, assign the sysadmin role to your user, exit and remove the -m parameter and restart sql server.

4
  • if I do this I can't log in to my sql server management studio, do you have any idea why this might be the case?
    – Jeremy C.
    Jun 11, 2015 at 7:21
  • Maybe there are a running service (o remote connection) that login to SQL instead of you and get the ONLY connection.
    – Max
    Jun 11, 2015 at 8:06
  • 1
    worst suggestion for me. Messed up my users, database. Have to do a full re-install. Urg! Apr 15, 2016 at 16:23
  • first try the easier solution of checking the database service Jun 21, 2016 at 7:15
34

The problem is that the user in the database is an "orphan". This means that there is no login id or password associated with the user. This is true even if there is a login id that matches the user, since there is a GUID (called a SID in Microsoft-speak) that has to match as well.

This used to be a pain to fix, but currently (SQL Server 2000, SP3) there is a stored procedure that does the heavy lifting.

All of these instructions should be done as a database admin, with the restored database selected.

First, make sure that this is the problem. This will lists the orphaned users:

EXEC sp_change_users_login 'Report'

If you already have a login id and password for this user, fix it by doing:

EXEC sp_change_users_login 'Auto_Fix', 'user'

If you want to create a new login id and password for this user, fix it by doing:

EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'

this text was obtained at http://www.fileformat.info/tip/microsoft/sql_orphan_user.htm in Dez-13-2017

2
  • 1
    This worked for me. Make sure to replace 'user' with the orphaned username, if you (as me) have no idea what's going on :) Apr 4, 2019 at 13:26
  • This worked for me after I switched to the affected database using @Njal's answer below: USE [dbname] GO EXEC sp_change_users_login...
    – Pakman
    Jan 7, 2020 at 17:54
10

Really stupid solution but I'll add it here in case anyone gets here from a Google search.

I'd just restarted the SQL service and was getting this error and in my case, just waiting 10 minutes was enough and it was fine again. Seems this is the error you get when it is just starting up.

1
  • 2
    Strange ! But worked for me. Thanks a lot for sharing. Sep 23, 2019 at 9:20
5

If you are using Sql Management Studio, just start it as Administrator.

Right click->Run as Administrator

4

This is what led me to this issue and how I fixed it:

Restored my database to another SQL server instance from a .bak file, which included a preexisting user.

Tried to access the restored database from my app as usual using the same connection string but updated server instance.

Received error.

Deleted user as the DBowner, then readded with exact same credentials, mappings, login, etc.

Was able to login as the user after readding the user after the restore.

4

I had twoo users: one that had the sysadmin role, the other one (the problematic one) didn't.

So I logged in with the other user(you can create a new one) and checked the ckeck box 'sysadmin' from: Security --> Logins --> Right ckick on your SQL user name --> Properties --> Server Roles --> make sure that the 'sysadmin' checkbox has the check mark. Press OK and try connecting with the newly checked user.

3

This is caused when the user's default database is set to a database they don't have permissions or its offline.

Just try to re add the user.Pleae have a look here too.

1
  • 1
    Tried to re-add the user and I got the error, while dropping. Cannot drop the existing login DROPPING THE LOGIN DELETE FAILURE TITLE: Microsoft SQL Server Management Studio ------------------------------ Drop failed for Login 'IRON\Manoj_2'. (Microsoft.SqlServer.Smo) An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) Cannot drop the login 'IRON\Manoj_2', because it does not exist or you do not have permission. (Microsoft SQL Server, Error: 15151) Oct 5, 2014 at 8:18
2

This fixed it for me:

Use [dbName]
GO

EXEC sp_change_users_login 'Auto_Fix','Manoj', null, 'Manojspassword'
GO
2

In my case it worked when I had opened SQL Server Management Studio with Administrator credentials and I right-clicked on the database and select "Go online" or something like this.

1

Please try this script.. What this script does is it looks at the active sessions of the database and kills them so you can bring the database back online.

 CREATE TABLE #temp_sp_who2
        (
          SPID INT,
          Status VARCHAR(1000) NULL,
          Login SYSNAME NULL,
          HostName SYSNAME NULL,
          BlkBy SYSNAME NULL,
          DBName SYSNAME NULL,
          Command VARCHAR(1000) NULL,
          CPUTime INT NULL,
          DiskIO INT NULL,
          LastBatch VARCHAR(1000) NULL,
          ProgramName VARCHAR(1000) NULL,
          SPID2 INT
          , rEQUESTID INT NULL --comment out for SQL 2000 databases

        )


    INSERT  INTO #temp_sp_who2
    EXEC sp_who2


    declare @kill nvarchar(max)= ''
    SELECT  @kill = @kill+ 'kill '+convert(varchar,spid) +';'
    FROM    #temp_sp_who2
    WHERE   DBName = 'databasename'

    exec sp_executesql @kill


  ALTER DATABASE DATABASENAME SET ONLINE WITH IMMEDIATE ROLLBACK
1

In my case, I simply had to start the application with "Run as administrator" in order to access anything. Otherwise I'd get the error you mentioned.

0
1

Get this error in this steps:

  1. Run "Get offline".
  2. "Get offline" was running too long, so i closed this window.
  3. Then i got this error.

Steps to fix:

  1. Go to "Activity monitor" and delete all connections to this db. Then DB became really offline and all is ok.
1

In my case, restarting SQL Server Service was enough to resolve the issue.

1

I experienced a similar problem after running a few jobs of bulk insert through a Python script on a separate machine and a separate user from the one I am logging in to SSMS.

It appears that if the Python kernel (or possibly any other connection) is interrupted in the middle of a bulk insert job without properly 'cleaning up' the mess, some sort of hanging related to user credentials and locks may happen on the SQL Server side. Neither restarting the service nor the whole machine worked for me.

The solution in my case was to take the DB offline and online. In the SQL Server Management Studio, that is a right click on DB > tasks > take offline and then right click on DB > tasks > bring online.

0

My issue got resolved by restarting the MS SQL server service, simple.

0

I had a similar problem, for me I had to create a new user with name that I needed, in your case you should create some like this:

USE [master]

GO

/****** Object:  Login [Manoj_2]    Script Date: 9/5/2019 12:16:14 PM ******/
CREATE LOGIN [Manoj_2] FROM WINDOWS WITH DEFAULT_DATABASE=[master],
DEFAULT_LANGUAGE=[us_english]

GO

ALTER SERVER ROLE [sysadmin] ADD MEMBER [Manoj_2]
GO
0

Execute the following sentence:

EXEC rdsadmin.dbo.rds_set_database_online dbname
0

in my case I was restoring a huge DB, and immediately trying to connect to it. I just had to wait about 30 mins and got this working.

enter image description here

-1

I performed the below steps and it worked for me:

1) connect to SQL Server->Security->logins->search for the particular user->Properties->server Roles-> enable "sys admin" check box

1
  • @DrewB That's correct, but don't you think it works for the users who are actual admins in the organization or the rights can be given to a particular group where its defined as Admins
    – Docgyan
    Sep 26, 2017 at 15:11
-1

I just restarted my SQL Server (MSSQLSERVER) with which my SQL Server Agent (MSSQLSERVER) also got restarted. Now am able to access the SQL SERVER 2008 R2 database instance through SSMS with my login.

-2

Issue: The database [dbName] is not accessible. (ObjectExplorer) got the error when expanding the database.

Solution: Deattach the database > Drop Option Attach the database again with the mdf file under the mssql data folder

-2

Go to

Security >> Logins >> Right click to the user >> Properties >>

On the left navigation move to >> User Mapping >> Check the database and in the "Database role membership for: <>" check "db_owner" for user that you are experience the issue.

PROBLEM SOLVED...

2
  • 5
    Unnecessarily granting a user admin or owner rights is a horrible idea security-wise.
    – DrewB
    Sep 25, 2017 at 12:28
  • This does NOT WORK
    – A X
    Feb 20, 2021 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.