Search code examples
databasesql-server-2005database-restore

Restore SQL Server 2005 database without overwriting DB users


I have a database that I constantly need to restore.

Every time I do so, all the users that have access to DB are deleted and I have to manually add them again.

Is there any way that I can restore the DB but the users remain intact or any script that I run before/after the restore so that I don't have to manually add all the users again?


Solution

  • Before the restore, you can script the users as they currently exist. In object explorer, expand the database, expand security, highlight the users node and click f8 (object explorer details). In object explorer details, select the users you want to keep, right click and script as create to new window, clipboard etc.

    Now run the restore, and when the restore is complete, run the script from above. If this is all happening on the same server, should work just fine as I described. If these are different servers, you may want to look into synchronizing server-level logins first.