Tech Junkie Blog - Real World Tutorials, Happy Coding!: SQL Server : Attaching A Database File to SQL Server 2012

Saturday, September 7, 2019

SQL Server : Attaching A Database File to SQL Server 2012

There are times when you are handed over an .mdf file and are tasked to create a new database out of it.  There's no log just an .mdf file.  The easiest way to create a new database is to use the query editor in SQL Management Studio type the following into the Sql Query window

CREATE DATABASE Northwind
 ON (name= 'Northwind',
 filename = 'C:\Northwind.mdf')
FOR ATTACH_REBUILD_LOG;

The query above will create a new database for you call "Northwind", all you have to do is specify the location of the .mdf file and the "FOR ATTACH_REBUILD_LOG" will rebuild the log for you. You might run into a "Access denied" error but it's a permissions issue. Make sure the user that is running the query has permission to the file.


2 comments:

  1. I was working on a project that used SQL Server 2012, and I came across this guide on attaching a database file. I followed the steps, but I found that some of the database tasks were taking longer than I expected. During my search for additional solutions, I found Dissertation Conclusion Help, which improved my understanding of how to structure my approach. It's great to see such detailed instructions for SQL Server.

    ReplyDelete

Search This Blog