Thursday, September 13, 2007

Changing in Mass the Default database for Users

Alright, this little quick and dirty script will mass assign all the users (with an exception of some listed below) to a specified default database. I ran this on SQL Server 2000 but it should work on SQL 2005.



--BEGIN SCRIPT--

declare @user varchar(30)

--create the cursor.. but take out of the list of users that I don't want to change the default
--database for. Hopefully you can fine tune this list down.
declare c_users cursor for
select name from syslogins
where name NOT IN('sa', ' 'readonly', 'BUILTIN\Administrators', 'QA\Administrator' )


open c_users
fetch next from c_users into
@user

while @@fetch_Status = 0
begin
print 'defaulting user '+@user
--PUT IN Default Database here below!
exec sp_defaultdb @user, MY_Default_Database -- <--put your default database right here
fetch next from c_users into
@user

end
close c_users
deallocate c_users



--END SCRIPT--

No comments: