The other day, I had the need to attach to a Microsoft SQL Server database that a customer had sent on CD. Not a backup mind you, just the raw files. Took me a little while to figure out this puzzler, so I figured I'd post it here in case you had run into the same thing.

You'll want to run a stored procedure called sp_attach_db, which the documentation says is designed for databases that have been detached. Might be that the database needs to be detached in order to allow you to re-attach it, I'm not sure. For me, I had no problem attaching this database, which may or may not have been detached on the other end.

 EXEC sp_attach_db @dbname = N'databasename', 
  @filename1 = N'pathtodatafile.mdf', 
  @filename2 = N'pathtologfile.ldf'

After executing, I was able to access the new database without a problem. Sweet!

Leave a comment