Apologies for the newbie question - I've got a .mdf file, and I've no idea how to run queries against it. I've downloaded and installed SQL Server Express 2005, but don't know when to start. I know that I'm not meant to open the file directly, but don't know what I am meant to do.

Could someone post a step by step of how to get to the point of running SQL queries against the file?


Solution 1:

After you have installed SQL Server Express Edition/SQL Management Studio Express and tried the answers mentioned by other posters and still have problems attaching due to no log file the following command may be of use to you:

sp_attach_single_file_db @dbname= 'ENTER_DB_NAME', @physname= 'SOMEDRIVE:\SOME\LOCATION\TO\THE\DATABASEFILE.mdf'

This will attach the database and create a blank logfile for it.

Hope this helps you!

Solution 2:

Assuming it's an SQL Server 2005 (or earlier) database file, you can "Attach" the database to a running instance of SQL Server 2005.

Open SQL Management Studio, connect to the database server, expand the server computer node, right-click "Databases" and choose "Attach...". Click "Add" in the top pane of the "Attach Databases" window and browse to the MDF file (which must be stored locally on the computer SQL Server is running on).

Solution 3:

With a bit of luck you'll be able to attach the database. You need SQL Studio Management Studio Express installed so you can configure your SQL Server. Once you've opened up the Management Studio and connected to your SQL Server right click "Databases" and click "Attach...". Then browse your way to the .mdf file.

If it attaches you should be able to drill down into the database and see the tables, views etc. You can then run queries within Management Studio.

Not all .mdf files will attach. Try it and see.

JR

Solution 4:

What you are looking for is probably the term, Attaching database

Here is a good tutorial on how to attach an .MDF file (with step-by-step pictures!) here It is for SQL Server 2008, but the steps are the same in SQL Server 2005