SQL Server 2005 does not support DBCC REBUILD_LOG. So, we cannot use it to rebuild the LDF. Here is the process to attach a database only using MDF without LDF.
VS 2005 doesn't had this facility so we use VS 2008. This method can also wok with Visual Web Developer 2008 Express Edition.
Open VS.2008 and do the followings:
If you are only interested in attaching MDF to the SQL 2005 server without data extraction and replication.
Here is another way to attach MDF file to the SQL 2005 sever without LDF. In this approach, you don't need to install Visual Studio but you may need another tool for help depending on your result by running the following SQL statement.
In the following example, we will use AdventureWorks.mdf as an example.
Execute the following statement to attach your MDF on SQL Management Studio:
If everything works fine, you may get the similar message below and have your database attached after execution:
Step 1: Use SSEUtil to attach MDF into the database server
Step 2: Add MDF into sysdatabases
Ref: http://blog-rat.blogspot.com
VS 2005 doesn't had this facility so we use VS 2008. This method can also wok with Visual Web Developer 2008 Express Edition.
Open VS.2008 and do the followings:
- Open a new Web site or use the existing one.
- Add the file to the project's App_Data folder.
- Right click this folder in the Solution Explorer and select Add Existing Item.
- Locate the MDF file you want to use and click on Add.
- Double-click on the MDF file that you just added; it will open Server Explorer for you.
- Locate and right-click the MDF file data connection on Server Explorer.
- Select Publish to provider...
- Go through the wizard; when the wizard asks you where to publish, and select Script to file. It will generate a SQL file that contains all the database schema and its data.
- Create a database with the same name as your MDF.
- Run the SQL file you just created by Server Explorer in VS.2008.
If you are only interested in attaching MDF to the SQL 2005 server without data extraction and replication.
Here is another way to attach MDF file to the SQL 2005 sever without LDF. In this approach, you don't need to install Visual Studio but you may need another tool for help depending on your result by running the following SQL statement.
In the following example, we will use AdventureWorks.mdf as an example.
Execute the following statement to attach your MDF on SQL Management Studio:
USE master
GO
CREATE DATABASE AdventureWorks
ON PRIMARY (FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks.mdf')
FOR ATTACH_REBUILD_LOG
GO
GO
CREATE DATABASE AdventureWorks
ON PRIMARY (FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks.mdf')
FOR ATTACH_REBUILD_LOG
GO
If everything works fine, you may get the similar message below and have your database attached after execution:
- File activation failure. The physical file name "D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_log.ldf" may be incorrect. New log file 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_log.LDF' was created.
- File activation failure. The physical file name "D:\Users\<SomeUserName>\Documents\MyCode\Test\App_Data\AdventureWorks_log.ldf" may be incorrect. The log cannot be rebuilt when the primary file is read-only. Msg 1813, Level 16, State 2, Line 3 Could not open new database 'AdventureWorks'. CREATE DATABASE is aborted.
Step 1: Use SSEUtil to attach MDF into the database server
First, you can copy the MDF to the location where you usually store for your SQL server if you like, e.g.,C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\, or keep wherever it is now.
Then at the command prompt, run sseutil's attach database command. Before doing it, please be sure to add your path to sseutil.exe. Otherwise, you need to use its absolute path name,., e.g., c:\util\sseutil.exe.
or
You can use sseutil console to interact with the server.
Then at the command prompt, run sseutil's attach database command. Before doing it, please be sure to add your path to sseutil.exe. Otherwise, you need to use its absolute path name,., e.g., c:\util\sseutil.exe.
- sseutil -s .\SQLExpress -a "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks.mdf" AdverureWorks
- -s to specify the instance of SQL server.
- -a to attach the database by specifying its MDF file path name.
- The last parameter is optional. It is the name of database you want to call (e.g., AdvantureWorks).
or
You can use sseutil console to interact with the server.
- To start a sseutil console, at the command prompt, type
sseutil -c - You will see
Console mode. Type 'help' for more information.
1> - To attach a MDF, type
!attach "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks.mdf"AdventureWorks - Type quit when you're done. This step is necessary to enable you to add the database into the sysdabases in the next step by removing the lock on file.
Step 2: Add MDF into sysdatabases
Now you have MDF attached to the server and have LDF recreated. But the database will still remain hidden from SQL Server Management Studio. In order to make it visible in SQL Server Management Studio, you need to go through the database attach wizard to add the database back to sysdatabases; or in the management studio, run either one of the following SQL statements:
I hope that you find this guide useful.- USE master
GO
EXEC SP_ATTACH_DB
@filename1=N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks.mdf',
@dbname=N'AdventureWorks
or
- USE master
GO
CREATE DATABASE AdventureWorks
ON PRIMARY (FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks.mdf')
FOR ATTACH
GO
Ref: http://blog-rat.blogspot.com
Nice post very helpful
ReplyDeletedbakings
If the mdf file is damage in my computer then sql database recovery is must because all the data and records are stored in mdf file. This is the important file for us so i repair the mdf file for sql database recovery.
ReplyDeleteBasically there are many reasons which are responsible for SQL database corruption like hardware failure, improper power supply, virus attack and more but such issues in SQL Server database can be more easily with the help of SQL Database Repair software which can recovers the database from corrupt SQL MDF & NDF files and saves them in SQL Server.
ReplyDeleteRead more:- http://www.filesrecoverytool.com/sql-database-repair.html
This comment has been removed by the author.
ReplyDeleteTry SQL Database Recovery application and retrieve MDF and NDF file from SQL server database.
ReplyDeleteThanks for sharing this informative article with us. I have found another helpful article that show step by step methods to attach mdf file without ldf file using SQL Server Management Studio: http://www.sqlserverlogexplorer.com/how-to-attach-mdf-file-without-ldf-file/
ReplyDelete