views:

682

answers:

2

I have backed up and restored a MS SQL Server 2005 database to a new server.

What is the best way of recreating the login, the users, and the user permissions?

On SQL Server 2000's Enterprise Manager I was able to script the logins, script the users and script the user permissions all seperately. I could then run one after the other and the only remaining manual step was to set the login password (which do not script for security reasons)

This does not seem possible in SQL Server 2005's Management Studio, making everything very fiddly and time consuming. (I end up having to script the whole database, delete all logins and users from the new database, run the script, and then trawl through a mixture of error message to see what worked and what didn't.)

Does anyone have any experience and recommendations on this?

A: 

I use the SQL Compare product from Red Gate (http://www.red-gate.com/products/SQL_Compare/index.htm). There are other similar products around but I've had no reason to look for one as SQL Compare has never let me down.

You'll find it is useful for a lot more than the your current requirement as it will help synchronize all types of database object, not just login and permissions.

Elliveny
We have a similar "database differential" tool. Unfortunately our Client does not...
Dems
+5  A: 

The easiest way to do this is with Microsoft's sp_help _revlogin (had to put a space in there to fool the formatting), a stored procedure that scripts all SQL Server logins, defaults and passwords, and keeps the same SIDs.

You can find it in this knowledge base article:

http://support.microsoft.com/kb/918992

Brent Ozar
Doesn't appear to do the user permissions though. (Such as GRANT EXECUTE and GRANT SELECT, etc, etc.)
Dems
Right, because that's contained in each individual database, not at the server level. If you back up a database, do sp_help_revlogin on the old server, use that generated T-SQL on the new server, and then do the restores, you're done.
Brent Ozar
The "users" in the restored database still appear to be orphaned from their "logins".
Dems
That's because you didn't do it in order. You have to create the users via sp_help_revlogin BEFORE restoring the database. If the users exist now, drop them because they have the wrong sids.
Brent Ozar