SQL SERVER – FIX : Error 15023: User already exists in current database.

Error 15023: User already exists in current database.

1) This is the best Solution.
First of all run following T-SQL Query in Query Analyzer. This will return all the existing users in database in result pan.
USE YourDB
GO
EXEC sp_change_users_login 'Report'
GO

Run following T-SQL Query in Query Analyzer to associate login with the username. ‘Auto_Fix’ attribute will create the user in SQL Server instance if it does not exist. In following example ‘ColdFusion’ is UserName, ‘cf’ is Password. Auto-Fix links a user entry in the sysusers table in the current database to a login of the same name in sysxlogins.
USE YourDB
GO
EXEC sp_change_users_login 'Auto_Fix', 'ColdFusion', NULL, 'cf'
GO

Run following T-SQL Query in Query Analyzer to associate login with the username. ‘Update_One’ links the specified user in the current database to login. login must already exist. user and login must be specified. password must be NULL or not specified
USE YourDB
GO
EXEC sp_change_users_login 'update_one', 'ColdFusion', 'ColdFusion'
GO

2) If login account has permission to drop other users, run following T-SQL in Query Analyzer. This will drop the user.
USE YourDB
GO
EXEC sp_dropuser 'ColdFusion'
GO

Create the same user again in the database without any error.

Stored Procedure 1:
/*Following Stored Procedure will fix all the Orphan users in database
by mapping them to username already exist for user on server.
This SP is required when user has been created at server level but does
not show up as user in database.*/
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

Stored Procedure 2:
/*Following Stored Procedure will fix all the Orphan users in database
by creating the server level user selecting same password as username.
Make sure that you change all the password once users are created*/
CREATE PROCEDURE dbo.spDBA_FixOrphanUsersPassWord
AS
DECLARE
@username VARCHAR(25)
DECLARE @password 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
SET @password = @username
WHILE @@FETCH_STATUS = 0
BEGIN
IF
@username='dbo'
EXEC sp_changedbowner 'sa'
ELSE
EXEC
sp_change_users_login 'Auto_Fix', @username, NULL, @password
FETCH NEXT
FROM GetOrphanUsers
INTO @username
END
CLOSE
GetOrphanUsers
DEALLOCATE GetOrphanUsers
GO

Stored Procedure 3:
----Following Stored Procedure will drop all the Orphan users in database.
----If you need any of those users, you can create them again.
CREATE PROCEDURE dbo.spDBA_DropOrphanUsers
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_dropuser @username
FETCH NEXT
FROM GetOrphanUsers
INTO @username
END
CLOSE
GetOrphanUsers
DEALLOCATE GetOrphanUsers
GO

Reference : Pinal Dave (https://blog.sqlauthority.com )

DBA, SQL Cursor, SQL Download, SQL Error Messages, SQL Joins, SQL Scripts, SQL Server DBCC, SQL Server Security, SQL Stored Procedure
Previous Post
SQL SERVER – Function to Convert List to Table
Next Post
SQL SERVER – Creating Comma Separate List From Table

Related Posts

129 Comments. Leave new

  • Thanks Mate… Comprehenssive one…

    Reply
  • He aqui una forma mas rapida para usuarios menos avanzados.

    En la Tabla Sysusers, de tu DB, se almacenan todos los usuarios con acceso a ella, solo tienes que eliminar el que te da el error y luego volver a crearlo por la via correspondiente.

    Espero le sirva, cualquier comentario me dejan saber.

    Reply
  • Here is a fastest way to less advanced users.

    Table sysusers, your DB, are stored all users with access to it, just eliminate that gives you the error and then recreate it through the appropriate channels.

    I hope you serve, any comments let me know

    Reply
  • How to avoid dropping the db user without login (new feature in 2005) while dropping the orphaned users in a database?

    Reply
  • Thanks! This was a big help.

    Reply
  • Martin Knazovicky
    April 15, 2011 4:45 am

    Nice – simple and effective.
    EXEC sp_change_users_login ‘Report’
    EXEC sp_change_users_login ‘Auto_Fix’, ‘ColdFusion’, NULL, ‘cf’
    EXEC sp_change_users_login ‘update_one’, ‘ColdFusion’, ‘ColdFusion’

    fixed mine problem after I created new DB from backup.

    Reply
  • Gagan Bhargava
    May 3, 2011 8:34 am

    Thanks a lot for this great help….. Absolutely wonderful :)

    Reply
  • Thanks alot, it’s really helpful

    Reply
  • Thanks for the post! This really fit the bill in my case….

    Reply
  • very nice. ;-)

    Reply
  • The first “best solution” was very useful on Windows Server 2008, IIS7, SQL Server 2008. Thank you!

    Reply
  • Merci pour votre fix. J’ai utilisé la première solution avec succès.

    Thanks for your help!

    Reply
  • I am facing one issue while installing sql 2008 standard edition i get following error message continuously tried many times uninstalling, m installing on windows 2003 standard edition and it is a backup domain controller, few week back i have installed successfully one of our backup domain controller without any problem.

    Please can anyone help me solving this issue ?

    Reply
  • Thank you very much. You’re always straight to the point.

    Reply
  • Thanks, just what I had been looking for.

    Reply
  • thanks a lot !!

    Reply
  • Excellent!!!
    It made wonders…

    Thanks a lot

    Reply
  • That’s working!!! Thanks

    Reply
  • Simply brilliant, thank you very much, indeed.

    Reply
  • Thanks a zillion. It works like a gem.

    Reply

Leave a Reply