declare @LOGINNAME varchar(100) declare @CHANGECOMMAND varchar(100) declare USERCURSOR cursor for select name from sysusers where name not in ( ' public' , 'dbo' , 'guest' , 'sys' , 'Information_schema' , 'db_owner' , 'db_accessadmin' , 'db_securityadmin' , 'db_ddladmin' , 'db_backupoperator' , 'db_datareader' , 'db_datawriter' , 'db_denydatareader' , 'db_denydatawriter') open USERCURSOR fetch next from USERCURSOR into @LOGINNAME while @@fetch_status=0 begin select @CHANGECOMMAND = 'exec sp_change_users_login ''Auto_Fix'', ''' +@LOGINNAME + '''' PRINT 'Executing the command '+@CHANGECOMMAND exec (@CHANGECOMMAND ) fetch next from USERCURSOR into @LOGINNAME end close USERCURSOR deallocate USERCURSOR