Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

Monday, April 12, 2010

Script for fixing orphan users

As a follow up to my previous post for fixing orphan users,Here is the script that will allow to fix orphan users in a database

Declare @sql varchar(5000)

select @sql = coalesce (@sql ,'') + 'ALTER USER '+ name + ' WITH LOGIN = '+ name + ';'
from sysusers
where issqluser = 1
and (sid is not null and sid <> 0x0)
and (len(sid) <= 16) and suser_sname(sid) is null order by name Print @sql

Exec @sql