Last active
October 31, 2016 17:11
-
-
Save kdarty/73a3335a0206224c2752939a6f48b4b2 to your computer and use it in GitHub Desktop.
With Server Moves and Database Restores from one Database Server to another, it is easy for User Accounts to get "orphaned" where their associated SID (GUID) no longer matches the deployed Server. To fix this, you need to reset or re-sync the Accounts. This is a good solution I found on Stackoverflow which works rather well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Source: http://stackoverflow.com/questions/19009488/the-server-principal-is-not-able-to-access-the-database-under-the-current-securi | |
USE [your database] | |
GO | |
-------- Reset SQL user account guids --------------------- | |
DECLARE @UserName nvarchar(255) | |
DECLARE orphanuser_cur cursor for | |
SELECT UserName = su.name | |
FROM sysusers su | |
JOIN sys.server_principals sp ON sp.name = su.name | |
WHERE issqluser = 1 AND | |
(su.sid IS NOT NULL AND su.sid <> 0x0) AND | |
suser_sname(su.sid) is null | |
ORDER BY su.name | |
OPEN orphanuser_cur | |
FETCH NEXT FROM orphanuser_cur INTO @UserName | |
WHILE (@@fetch_status = 0) | |
BEGIN | |
--PRINT @UserName + ' user name being resynced' | |
exec sp_change_users_login 'Update_one', @UserName, @UserName | |
FETCH NEXT FROM orphanuser_cur INTO @UserName | |
END | |
CLOSE orphanuser_cur | |
DEALLOCATE orphanuser_cur |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment