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

  • Brilliant

    Reply
  • Wonderful! That solved the problem. Thank you so much.

    Reply
  • a quick resolution to my problem. You’re totally awesome

    Reply
  • Your the best! Fixed my confluence problem, been struggling with it all day!

    Reply
  • Fixed my problem, thank you. You are brilliant.

    Reply
  • Your post helped me a lot,Thank you.

    Reply
  • Thanks a lot!! It solved my problem!!

    Reply
  • Great, worked perfectly, solved my problem. Thanks alot

    Reply
  • dbo is a forbidden value for the login name in the procedure

    Reply
  • I get the “new database owner is already a user” error, but when I try to run the

    EXEC sp_change_users_login ‘Report’ command,
    I
    get no data returned. The Query completes successfully, but the panel is empty. I don’t understand where to go from here.

    Thanks for your assistance.

    Reply
  • Great article.. Saved my day

    Reply
  • Chandru Maruthapan
    March 18, 2013 9:00 am

    Thanks Pinal,
    for understand the problem and create the script for myself for this one..

    USE Mirror
    GO
    DECLARE @str_query VARCHAR(max);
    DECLARE @un VARCHAR(50);

    CREATE TABLE #TempTable (name varchar(25))
    INSERT INTO #TempTable (name)

    SELECT SU.NAME AS UserName
    FROM sys.sysusers AS SU LEFT JOIN sys.server_principals AS SP ON SU.name = SP.name
    AND SP.type = ‘S’
    WHERE SU.issqluser = 1 — Only SQL logins
    AND NOT SU.[sid] IS NULL — Exclude system user
    AND SU.sid 0x0 — Exclude guest account
    AND LEN(SU.sid) <= 16 — Exclude Windows accounts & roles
    AND SUSER_SNAME(SU.sid) IS NULL — Login for SID is null

    DECLARE cur CURSOR FOR
    SELECT name FROM #TempTable
    OPEN cur
    FETCH NEXT FROM cur INTO @un;
    WHILE @@FETCH_STATUS = 0
    BEGIN
    SET @str_query = 'EXEC sp_change_users_login ''AUTO_FIX'' , '''+@un+'''' + CHAR(10) + 'GO' + CHAR(10)
    EXEC (@str_query)
    –PRINT @str_query –TEST POINTER

    FETCH NEXT FROM cur INTO @un
    END
    CLOSE cur;
    DEALLOCATE cur;
    DROP TABLE #TempTable

    Reply
  • Im getting this message. I cant figure this out for the life of me. It wont let me delete the user since it owns a full text catalog in the database and I cant create user mappings since it already shows the user in the database. Please help its urgent. Thank you.

    Msg 15600, Level 15, State 1, Procedure sp_change_users_login, Line 214
    An invalid parameter or option was specified for procedure ‘sys.sp_change_users_login’.

    Reply
  • For everyone getting the “An invalid parameter or option was specified for procedure” Try:
    EXEC sp_change_users_login ‘Auto_Fix’, ‘username’, NULL, ‘password’

    Worked for me.

    Thanks!

    Reply
  • Thank you Pinal. This was very helpful.

    Reply
  • Ty so much

    Reply
  • Thanks a lot for your solution.. It helped me a lot :)

    Reply
  • Thanks!!! it was very helpful!

    Reply
  • Thank you. Worked like a charm.

    Reply
  • Ryan Pringnitz
    October 20, 2013 3:07 am

    This worked. Thanks

    Reply

Leave a Reply