I had an issue where each day a database would detach itself so I slimmed down another authors post (mentioned at the end of this post) for my needs.
I have only tried this on a Windows 2008 R2 server / SQL Server 2008R2 but i’m sure it would be fine on Windows 2003/SQL Server 2005..
- Create a batch file .e.g Attach_db.bat
- Copy the contents below into the file and replace the following to your own settings:- SERVERNAME, DATABASE, YOUR DATABASE NAME NO EXTENSION.MDF & DATABASE PATH.
- Detach the file from SQL Server and run the file to test it.
@echo off set host=%computername% @echo ---- Connecting to Server ---- REM -------------********* ATTACH DATABASE*********------------- sqlcmd -E -S SERVERNAME\DATABASE -d master -Q "EXEC sp_attach_db @dbname=N'YOUR DATABASE NAME NO EXTENSION.MDF', @filename1 = N'DATABASE PATH\YOUR DATABASE.mdf',@filename2 = N'DATABASE PATH\YOUR DATABASE_log.LDF'" @echo ---- Attached new db ---- timeout /t 2 /nobreak>nul
This is a slimmed down version from Parth Dave’s excellent post.