Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Restoring Database using MDF file only

Author  Topic 

umertahir
Posting Yak Master

154 Posts

Posted - 2008-12-16 : 05:10:00
I have a MDF that I cannot figure out how to reattach because :-

1) sp_detach_db was NOT used to cleanly dismount.
2) The LDF has been deleted and is unavailable.

So my question is - How do I reattach an MDF when it was NOT detached firstly and the corresponding LDF is missing.

I have looked everywhere for a full day and cannot find the answer to this seemingly straightforward procedure.

Most of the places, there are solutions given for SQL 2000 but not for 2005.

I have tried following solutions but they do not seem to be working:
Solution 1:
1. Backup the data (.mdf) file! Just in case. We take no responsibility for anything that happens following this procedure.
2. EXEC sp_detach_db 'dbname' -- this will detach the database from the server
3. Restart SQL Server
The database may still be seen in enterprise manager, but just ignore it.
4. Create a new database with the same name or a different name. You will have to use a different physical file name, which is fine.
5. Stop SQL Server.
6. Rename the new data file that was created to something else (ex: add.bak to the end)
7. Rename the old data file that you want to restore to the name of the newly created file (the same name as the file you changed in the step above)
8. Start SQL Server
Now the db will still be suspect but you now have a log file.
9. Switch to emergency mode on the database. You do this by doing the following:
1. Right click on the database root node in Enterprise manager and bring up the properties.
2. Under the Server Settings tab, check of "Allow modifications to be made directly to the system catalogs".
3. click ok
4. Now go to the master database and open the sysdatabases table.
5. Find the suspected database in here and modify the status column, setting it to: 32768. This will put it into emergency mode.
6. stop then start sql server
10. Now here's the tricky part and I'm not sure how this will work on a single install, i was lucky enough to have SQL Server 2000 installed. But anyways, open up the Import and Export Data (DTS) program from the start menu. And you want to copy data from the old database to a brand new one. Just copy tables and views.

Solution 2:
EXEC sp_attach_single_file_db @dbname = 'MyDb'
@physname = 'C:\mydb_Data.mdf'

Thanks in advance.

umertahir
Posting Yak Master

154 Posts

Posted - 2008-12-16 : 05:31:29
Any suggestions please?
Go to Top of Page

umertahir
Posting Yak Master

154 Posts

Posted - 2008-12-16 : 09:34:02
As there are no suggestions so I am thinking of using recovery tools available...two of them I found out are :

- Recovery Toolbox for SQL Server (http://www.recoverytoolbox.com/buy_sql.html)
- Kernel SQL Recovery (http://www.nucleustechnologies.com/buy-sql-recovery.php)


Has anyone used these softwares before?
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-12-16 : 09:43:57
quote:
Originally posted by umertahir

As there are no suggestions so I am thinking of using recovery tools available...two of them I found out are :

- Recovery Toolbox for SQL Server (http://www.recoverytoolbox.com/buy_sql.html)
- Kernel SQL Recovery (http://www.nucleustechnologies.com/buy-sql-recovery.php)


Has anyone used these softwares before?



So it didn't work with sp_attach_single_file_db .see create database For attach if that is helpful.Otherwise you need to recover from 3rd party tools.
Go to Top of Page

umertahir
Posting Yak Master

154 Posts

Posted - 2008-12-16 : 09:46:44
quote:
Originally posted by sodeep

quote:
Originally posted by umertahir

As there are no suggestions so I am thinking of using recovery tools available...two of them I found out are :

- Recovery Toolbox for SQL Server (http://www.recoverytoolbox.com/buy_sql.html)
- Kernel SQL Recovery (http://www.nucleustechnologies.com/buy-sql-recovery.php)


Has anyone used these softwares before?



So it didn't work with sp_attach_single_file_db .see create database For attach if that is helpful.Otherwise you need to recover from 3rd party tools.



neither of that worked, some errors were being returned so I'm using the above two softwares in their trial version to check if they recover the tables, stored procedures etc.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-12-16 : 09:52:11
what are the errors?
Go to Top of Page

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2008-12-16 : 10:53:09
Are the *.bak and *.trn files not available?
Go to Top of Page

umertahir
Posting Yak Master

154 Posts

Posted - 2008-12-17 : 05:39:01
quote:
Originally posted by darkdusky

Are the *.bak and *.trn files not available?



No, only .MDF is available, .LDF has been lost which was in a different drive, which is crashed now.
Go to Top of Page

umertahir
Posting Yak Master

154 Posts

Posted - 2008-12-17 : 06:03:55
quote:
Originally posted by sodeep

what are the errors?



I get these errors using the second solution


EXEC sp_attach_single_file_db @dbname = 'Fertility_extraction_test', @physname = 'F:\Fertility_extraction_test.mdf'



Msg 3415, Level 16, State 3, Line 1
Database 'Fertility_extraction_test' cannot be upgraded because it is read-only or has read-only files. Make the database or files writeable, and rerun recovery.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'Fertility_extraction_test'. CREATE DATABASE is aborted.
Go to Top of Page

jaypabs
Starting Member

1 Post

Posted - 2008-12-27 : 02:35:56
Hi,

Did you attach the database by right clicking the windows under the databases in your sql server manager then all task >> Attach Database?

http://www.sourcecodester.com - Download free source code.

quote:
Originally posted by umertahir

I have a MDF that I cannot figure out how to reattach because :-

1) sp_detach_db was NOT used to cleanly dismount.
2) The LDF has been deleted and is unavailable.

So my question is - How do I reattach an MDF when it was NOT detached firstly and the corresponding LDF is missing.

I have looked everywhere for a full day and cannot find the answer to this seemingly straightforward procedure.

Most of the places, there are solutions given for SQL 2000 but not for 2005.

I have tried following solutions but they do not seem to be working:
Solution 1:
1. Backup the data (.mdf) file! Just in case. We take no responsibility for anything that happens following this procedure.
2. EXEC sp_detach_db 'dbname' -- this will detach the database from the server
3. Restart SQL Server
The database may still be seen in enterprise manager, but just ignore it.
4. Create a new database with the same name or a different name. You will have to use a different physical file name, which is fine.
5. Stop SQL Server.
6. Rename the new data file that was created to something else (ex: add.bak to the end)
7. Rename the old data file that you want to restore to the name of the newly created file (the same name as the file you changed in the step above)
8. Start SQL Server
Now the db will still be suspect but you now have a log file.
9. Switch to emergency mode on the database. You do this by doing the following:
1. Right click on the database root node in Enterprise manager and bring up the properties.
2. Under the Server Settings tab, check of "Allow modifications to be made directly to the system catalogs".
3. click ok
4. Now go to the master database and open the sysdatabases table.
5. Find the suspected database in here and modify the status column, setting it to: 32768. This will put it into emergency mode.
6. stop then start sql server
10. Now here's the tricky part and I'm not sure how this will work on a single install, i was lucky enough to have SQL Server 2000 installed. But anyways, open up the Import and Export Data (DTS) program from the start menu. And you want to copy data from the old database to a brand new one. Just copy tables and views.

Solution 2:
EXEC sp_attach_single_file_db @dbname = 'MyDb'
@physname = 'C:\mydb_Data.mdf'

Thanks in advance.

Go to Top of Page
   

- Advertisement -