declare @dbname varchar(500) declare @uname varchar(500) declare @string varchar(8000) declare user_delete cursor for select name from sysusers where name not in('public', 'guest', 'sys', 'db_accessadmin', 'db_backupoperator', 'db_datareader', 'db_datawriter', 'db_ddladmin', 'db_denydatareader', 'db_denydatawriter', 'db_owner', 'db_securityadmin', 'dbo', 'INFORMATION_SCHEMA') and name not like '%MSRep%' and name not like '%MStran%' and name not like '%\%' Order by name Open user_delete FETCH NEXT FROM user_delete INTO @uname WHILE @@FETCH_STATUS=0 BEGIN set @string=+char(13)+char(10) +'USE CHECKDB ' +char(13)+char(10) +'DROP USER '+'['+@uname+']' --exec (@string) print (@string) FETCH NEXT FROM user_delete INTO @uname END CLOSE user_delete; DEALLOCATE user_delete; --select ''''+name+''',' from sysusers --order by name