Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 3 down vote accepted

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!

share|improve this answer
    
Thanks Wayne, that did the trick. –  Dominic Rodger Jun 25 '09 at 9:24
    
I thought you would need that as I have had to do exactly the same from a db provided by a 3rd party –  Wayne Jun 25 '09 at 11:15

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).

share|improve this answer
1  
Sounds good to me, but he might need to download SSMS to do it: microsoft.com/downloadS/… –  JFV Jun 24 '09 at 16:45
    
Also see my comment about sp_attach_single_file_db when unable to attach due to no log file. –  Wayne Jun 24 '09 at 23:41

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

share|improve this answer

This MSDN will help you get the database attached to your SQL server instance:

http://msdn.microsoft.com/en-us/library/ms165673.aspx

Once it's attached you should be able to run your queries against that database name.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.