SQL Server - T-SQL script to attach database with multiple files
Last week I was working on one of our regular database restore tasks and as a part of it, I was also required to detach and attach the database (with around 100 additional data files) on different SQL server instance. Now, if your source and destination servers have same disk partition and folder hierarchy, than attaching a database is quite a straight job. All you need to do is just copy all the database files from source server to same named folder and under same folder hierarchy on the destination server and then attach the database using the SSMS or T-SQL statement pointing to the primary data (mdf) file only and you're done. SQL Server will take care of listing and attaching the remaining files. But the tricky part comes in when destination server doesn't have same (name of the) disk partition or can't use the same folder/database file path (like may be same disk partition on the destination server doesn't have enough space to host that database). In that