Created
November 8, 2011 13:10
-
-
Save slabad/1347696 to your computer and use it in GitHub Desktop.
TSQL: FixOrphanUsers.sql
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
| DECLARE @name varchar(max) | |
| DECLARE @sql nvarchar(max) | |
| DECLARE name CURSOR FOR | |
| select dp.name from sys.database_principals dp | |
| join sys.syslogins l on l.name = dp.name | |
| OPEN name | |
| FETCH NEXT FROM name INTO @name | |
| WHILE @@FETCH_STATUS = 0 | |
| BEGIN | |
| SET @sql = 'ALTER USER [' + @name + '] WITH LOGIN = [' + @name + ']' | |
| -- SELECT @sql | |
| exec sp_executesql @sql | |
| FETCH NEXT FROM name INTO @name | |
| END | |
| CLOSE name | |
| DEALLOCATE name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment