Attach Existing Database Files to SQL Server
Use CREATE DATABASE ... FOR ATTACH when you have a complete database file set that was cleanly detached or copied while the source engine was safely offline. For routine migration of an active database, backup and restore usually provides a better operational path than detaching it.
Prepare the File Set
Use a compatible supported SQL Server instance, including Express where its limits fit the database. MSDE is a legacy product name, not a current deployment target. A newer engine can upgrade supported older database formats, but an upgraded database cannot simply be attached back to an older engine.
Keep an untouched backup before proceeding. Account for every data file, log file and special storage component. Do not copy MDF/LDF files from a running database and assume that the copy is consistent. For encrypted databases, arrange the required certificate or key on the destination first.
Place the files in the destination server's approved database storage. These are server-side paths, even when you run the command from a remote client. Grant the Database Engine service identity the required access to that location without granting broad filesystem permissions.
Attach the Files
Connect to the destination's master database using an account with the documented database-creation permissions. Confirm that the chosen database name is unused. Replace both paths below with the actual detached files and include any additional files in the ON list:
USE master;
GO
CREATE DATABASE [ArticleArchive]
ON
(FILENAME = N'D:\SqlData\ArticleArchive.mdf'),
(FILENAME = N'D:\SqlData\ArticleArchive_log.ldf')
FOR ATTACH;
GO
The command creates an attached database from existing files. It does not create an empty replacement. Run it outside an explicit transaction. Missing logs, access errors or version errors need diagnosis rather than an automatic ATTACH_REBUILD_LOG retry.
Verify State and Integrity
SELECT name, state_desc, compatibility_level
FROM sys.databases
WHERE name = N'ArticleArchive';
SELECT name, physical_name
FROM sys.master_files
WHERE database_id = DB_ID(N'ArticleArchive');
DBCC CHECKDB (N'ArticleArchive') WITH NO_INFOMSGS;
Expect one ONLINE database row, the intended file locations and no integrity errors. CHECKDB can be resource-intensive, so plan its execution appropriately for a large database.
Finally, verify users' login mappings, ownership, application connectivity and backup jobs. Attaching the database does not transfer server logins or other instance-level objects. Treat an unfamiliar database as executable content and inspect it in an isolated nonproduction environment before trusting it.
Microsoft References
Author: Herman Schoenfeld