Wednesday, February 20, 2013

User Group or Role already exists in the current database

When trying to create user with login or edit user mapping…etc, you may receive below error message.
Error 15023: User, Group or Role already exists in the current database
You may see below error when you are dealing with group

Error 15024: The group already exists in the current database
Users will receive this message when user existed without a SQLServer login in the database.

Run below command to see current user tologin mapping

Use[DatabaseName] ;
GO
exec sp_change_users_login @Action='Report' ;
GO

If the user does NOT exist, then you can run below command using “auto_fix” attribute to create the user in the database.

USE DB_NAME
GO
EXEC sp_change_users_login 'auto_fix', 'USER_NAME'
GO
You will see the output with following result
The row for user will be fixed by updating its login link to a login already in existence.
The number of orphaned users fixed by updating users was 1.
The number of orphaned users fixed by adding new logins and then updating users was 0.

If the user is ALREADY exists and if you want to map login with the user run below command using “updated_one” attribute.


USE DB_NAME
GO
EXEC sp_change_users_login 'update_one', 'USER_NAME', 'LOGIN_NAME’
GO
For Ex:-
USE TestDB; 
GO
EXEC sp_change_users_login 'Update_One', 'TestUser', 'TestUser'; 
GO 

You can also use below Stored Procedure to fix all the Orphan users in database by mapping them to username which is already exist on server. Make sure you test the script in a testing environment first.

CREATE PROCEDURE dbo.spDBA_FixOrphanUsers
AS
DECLARE @username VARCHAR(25)
DECLARE GetOrphanUsers CURSOR
FOR
SELECT UserName = name FROM sysusers
WHERE issqluser = 1
AND (sid IS NOT NULL AND sid <> 0x0)
AND SUSER_SNAME(sid) IS NULL ORDER BY name
OPEN GetOrphanUsers
FETCH NEXT
FROM GetOrphanUsers
INTO @username
WHILE @@FETCH_STATUS = 0
BEGIN
IF @username='dbo'
EXEC sp_changedbowner 'sa'
ELSE
EXEC sp_change_users_login 'update_one', @username, @username
FETCH NEXT
FROM GetOrphanUsers
INTO @username
END
CLOSE GetOrphanUsers
DEALLOCATE GetOrphanUsers
GO
You can refer Microsoft Link for more information
http://msdn.microsoft.com/en-us/library/ms175475.aspx

Regards,
Satishbabu Gunukula
http://www.sqlserver-expert.com

3 comments:

  1. Pretty nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed surfing around your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again very soon!fossil automatic watch

    ReplyDelete
  2. Thanks a lot for sharing this with all folks you actually know what you're talking about! Bookmarked. Please additionally visit my web site =). We will have a hyperlink alternate arrangement among us!buddhist funeral package

    ReplyDelete
  3. I had the problem when I was trying to copy the production to a local test database. In SSMS, I made sure to disconnect from the production before executing scripts on the local. However, even though I thought I had disconnected, the title of the production database was still there, and I got errors that objects were already there. The solution was to total exit from SSMS and start it again, only connecting to the local test database that time.

    ReplyDelete