Monday, April 30, 2007

Attaching databases to different Sql Servers

I often attach a SQL database created on my development machine to a production SQL server. When you do this you need to re-create the sql login accounts (SQL Server Management Studio -> Security -> Logins -> New Login...) on the target server and then run the following command to map the two accounts back together.

Open a query window from the attached database and run this command replacing DatabaseUsername and ServerUsername with your own values.

EXEC sp_change_users_login 'Update_One', 'DatabaseUsername', 'ServerUsername';