Re Sync SQL Server Logins with Users

–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’

To fix all orphaned users run:

>
USE —– change db name for which you want to fix orphan users issue
GO

declare @name varchar(150)
DECLARE cur CURSOR FOR
select name from master..syslogins
Open cur
FETCH NEXT FROM cur into @name
WHILE @@FETCH_STATUS = 0
BEGIN

EXEC sp_change_users_login 'AUTO_FIX', @name

FETCH NEXT FROM cur into @name

END

CLOSE cur
DEALLOCATE cur

>