In order to simplify things, I have developed a simple procedure that undertakes generating the necessary detach/attach scripts when you want to massively perform these actions on a SQL Server 2005 or later instance.
* Note: This procedure is presented in this article only for showing a different way of doing things. In any case where it could be used, a full backup of all the involved databases should be taken. Even though this method might be faster from backup/restore, the latter is much safer and preferable especially on Production environments.
The procedure for generating the scripts is the following:
- Run the “Attach DDL Statements Generation Scrip”
- Run the “Detach DDL Statements Generation Script”
If you want to proceed and detach all user databases you can then execute the set of DDL statements generated by the “Detach DDL Statements Generation Script”.
If you want to attach the databases on the same instance or any other SQL Server instance on the same server/PC you can then execute the set of DDL statements generated by the “Attach DDL Statements Generation Scrip”.
You can view the scripts below. You can also download it.
Attach DDL Statements Generation Scrip
Detach DDL Statements Generation Scrip