How to recover database from MDF in SQL Server 2005? - sql-server

I have an MDF file and no LDF files for a database created in MS SQL Server 2005. When I try to attach the MDF file to a different SQL Server, I get the following error message.
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
I would like to accomplish any one of the following options:
Attach the database without data loss (unlikely but would save me some time).
Attach the database with data loss (whatever transactions were open are lost).
Recover the schema only (no data) from the MDF file.
What SQL commands can I try to get my database going again?

I found the following document on Experts Exchange.
patrikt:
You will have data loss but it can be done.
1. Detach database and move your mdf to save location.
2. Create new databse of same name, same files, same file location and same file size.
3. Stop SQL server.
4. Swap mdf file of just created DB to your save one.
5. Start SQL. DB will go suspect.
6. ALTER DATABASE yourdb SET EMERGENCY
7. ALTER DATABASE yourdb SET SINGLE_USER
8. DBCC CHECKDB (yourdb, REPAIR_ALLOW_DATA_LOSS)
9. ALTER DATABASE yourdb SET MULTI_USER
10. ALTER DATABASE yourdb SET ONLINE

Here are details that cover parts 2) and 3) in case re-creating log doesn’t work which can happen if MDF file is corrupted.
You can recover data and structure only by reading MDF file with some third party tool that can de-code what’s written as binary data but even with such tools you can’t always do the job completely.
In such cases you can try ApexSQL Recover. From what I know this is the only tool that can do this kind of job but it’s quite expensive.
Much better idea is to try to recover these from any old backups if you have any.

FROM a post at SQL Server Forums Attaching MDF without LDF:
If you want to attach a MDF without LDF you can follow the steps below
It is tested and working fine
Create a new database with the same name and same MDF and LDF files
Stop sql server and rename the existing MDF to a new one and copy the original MDF to this location and delete the LDF files.
Start SQL Server
Now your database will be marked suspect 5. Update the sysdatabases to update to Emergency mode. This will not use LOG files in start up
Sp_configure "allow updates", 1
go
Reconfigure with override
GO
Update sysdatabases set status = 32768 where name = "BadDbName"
go
Sp_configure "allow updates", 0
go
Reconfigure with override
GO
Restart sql server. now the database will be in emergency mode
Now execute the undocumented DBCC to create a log file
DBCC REBUILD_LOG(dbname,'c:\dbname.ldf') -- Undocumented step to
create a new log file.
(replace the dbname and log file name based on ur requirement)
Execute sp_resetstatus
Restart SQL server and see the database is online.
UPDATE: DBCC REBUILD_LOG does not existing SQL2005 and above. This should work:
USE [master]
GO
CREATE DATABASE [Test] ON
(FILENAME = N'C:\MSSQL\Data\Test.mdf')
FOR ATTACH_REBUILD_LOG
GO

have you tried to ignore the ldf and just attach the mdf:
sp_attach_single_file_db [ #dbname = ] 'dbname' , [ #physname = ] 'physical_name'
i don't know exactly what will happen to your open transactions (probably just lost), but it might get your data back online.
-don

See here : Rebuild master and restore system databases from complete disk failure which has a very nice explanation

Just had this problem myself, but none of the above answers worked for me.
But instead, I found this which worked a treat and so I thought I'd share this for everyone else:
http://www.kodyaz.com/articles/sql-server-attach-database-mdf-file.aspx

Found a another way that works completely:
Create new database with same name to default database location.
Stop SQL server.
Copy old mdf file to overwrite newly created mdf file and delete new ldf file
Start SQL Server, database will be in emergency mode
Detach the emergency mode database
Copy original ldf file to default database location (where new LDF file as created and deleted under step 3 above.
Attach the database MDF file.
I got a working database after trying all of the above that failed for me.

I hope it is easy to do so,
Open SQL Server
Click New Query
Execute the following query
sp_attach_single_file_db #dbname='dbname',#physname='C:\Database\dbname.MDF'
Where dbname is you want to show in Object Explorer, where #physname is the local filepath location of your mdf file.
Hope it will help someone, i done by above, got both structure and also data.
Tested in Sql Server 2000 and 2008. In Sql Server 2000 it is not working, but works perfectly in 2008.

Related

Database restore is missing tables

I am using SSMS 2017 and attempting to restore a SQL Server 2016 backup (.bak) file to a new database (the data file and log file names in the original backup are different from the new database I am restoring to), so for example, db1.bak is being used to create a new database called db2 through a restore operation.
After the restore completes, all stored procedures and views are present, but none of the tables are there.
These are the steps I've taken in SSMS to perform the restore:
Right click the Databases folder
Select "Restore Database"
Under the General Page: For "Source" I choose Device and select the db1.bak file, and for "Destination" I enter the name of the new database to be created from the backup (db2)
Under the Files Page I select Relocate all files to folder (both Data and Log folder paths have been pre-populated with the correct names) as have the Logical File Name and the Restore As paths.
Under the Options Page I either select nothing or select Overwrite the existing database (WITH REPLACE).
Once the restore completes "successfully" I expand the newly created db2 and click to expand the tables and receive this error message:
What am I doing wrong?
Here is the script:
USE [master]
RESTORE DATABASE [PdsClone] FROM DISK = N'F:\SQLBackups\PDS_backup_2017_12_12_001015_0199267.bak' WITH FILE = 1, MOVE N'PDS_Data' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone.mdf', MOVE N'PDS_Log' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone_log.ldf', NOUNLOAD, REPLACE, STATS = 5 GO
UPDATE
I had the server group add another disk to that server and moved the SQL backups there, and I was able to successfully complete the restore this morning. I had previously cleaned off most of the backup files, but could it have been a space issue that prevented the tables from showing up in the restore? Every try it indicated success but no tables. They are there now, and I am thankful for your suggestions and help!
Not really an answer to what happened, but perhaps SSMS was doing something a little buggy as #JeroenMostert suggested.

SQL Server: How to attach / repair a detached / damaged database?

A database server crashed. I was able to get the mdf and the log file and I am trying to mount it on another server. I don't have backup as it was a development database.
When I try to attach the mdf and ldf files, Sql Server Management Studio give me this error:
TITLE: Microsoft SQL Server Management Studio
------------------------------
Attach database failed for Server
------------------------------
Could not redo log record (457:14202:19), for transaction ID (0:478674), on page (1:519205), database 'WSS_Content_5555' (database ID 15). Page: LSN = (370:463:113), type = 1. Log: OpCode = 2, context 2, PrevPageLSN: (298:40524:64).
Restore from a backup of the database, or repair the database.
During redoing of a logged operation in database 'WSS_Content_5555', an error occurred at log record ID (457:14202:19).
Typically, the specific failure is previously logged as an error in the Windows Event Log service.
Restore the database from a full backup, or repair the database.
Could not open new database 'WSS_Content_5555'.
CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 3456)
I don't know how to repair the database. Does it need to be attached before beeing repaired? In that case, how can I attach it?
You can try a workaround. In short:
Create a dummy DB with the same name (may need to remove the real DB first, save the original files or rename then).
Take the dummy off line (detach and (set offline or stop the SQL service)).
Delete dummy files, replace then with the real DB files.
Try to re-attach de DB
Edit
As by OP comment note you also can need to rebuild the log (if you lost transactions)
ALTER DATABASE [MyDatabase ] REBUILD LOG ON (NAME=’MyDatabaseLog’,FILENAME=’D:\Microsoft SQL Server\YourDataPath\Data\Logfile.ldf’)
and put the DB in multiple users log (taking the DB off can require you to put it in single use mode)
ALTER DATABASE [nomdb] SET MULTI_USER
For all the gore details you can refer to the Paul Randal Article
(Note in this article the author uses EMERGENCY MODE to (attempt) repair the transaction log)
I already used it with success but depending on the extent of the damage or others details it can be a impossible task. Consider restoring a backup.
Note this stunts are fine in a development server but you really need to plan (and drill) for disaster recovery in a prodution server.

Truncate SQL Server transaction log file

In my local C: drive I have a ldf file for a database that is hosted on one of our servers. I have a local copy of one of the databases that is 1 gb and a ldf (log file) of that database that is 16gb. It is eating up a lot of my local space on my hard drive. I would like to truncate that file. A lot of what I read online is don't, but it seems that they are talking about files on the server that the database is on. That isn't the case here, it is on my local machine.
The location of the file on my machine is:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA
How come I have a copy of that log file locally and also that database locally?
How would I go about truncating this file?
Thanks!
Go to the Object Explorer pane in SSMS and right click on the database in question. Choose tasks -> shrink -> files. Change the file type option to Log, click the "Reorganize pages before releasing unused space" option, and set the value to 1 MB. Hit OK.
If this doesn't work, check to see if your database is set up with a Full database recover model. Right click the database and go to properties. Choose Options, and check the Recover model option. Set to simple (if you can!!!), then shrink the logs.
The another option you can try is to use WITH TRUNCATE_ONLY:
BACKUP LOG databasename WITH TRUNCATE_ONLY
DBCC SHRINKFILE ( adventureworks_Log, 1)
but don't try this option in live environment, the far better option is to set database in simple recovery. see the below command to do this:
ALTER DATABASE mydatabase SET RECOVERY SIMPLE
DBCC SHRINKFILE (adventureworks_Log, 1)

howt to use .MDF file

work on SQL Server 2000.i have CustomerDetails_Data.MDF file .from this file i want to take all information on my database .How to do?
You will need to attach the .mdf data file to a database in SQL Server. Then you can simply query the information.
If you just have an .mdf file (and no log file .ldf), follow these steps to create a Database from your lone .mdf file:
Create a new database with the same name and same MDF and LDF files
Stop sql server and rename the existing MDF to a new one and copy the
original MDF to this location and
delete the LDF files.
Start SQL Server
Now your database will be marked suspect 5. Update the sysdatabases to
update to Emergency mode. This will
not use LOG files in start up
Sp_configure "allow updates", 1
go
Reconfigure with override
GO
Update sysdatabases set status = 32768 where name = "BadDbName"
go
Sp_configure "allow updates", 0
go
Reconfigure with override
GO
Restart sql server. now the database will be in emergency mode
Now execute the undocumented DBCC to create a log file
DBCC REBUILD_LOG(dbname,'c:\dbname.ldf') --
Undocumented step to create a new log
file.
(replace the dbname and log file name
based on your requirement)
Execute sp_resetstatus <dbname>
Restart SQL server and see the database is online.
You need to attach the .mdf data file to SQL Server, and SQL server will automatically generate a new LOG file, after that you can pass any query to the database...
What format do you want to extract the information to? You could write sql scripts against it, or use bulk copy.

How do I attach a MSSQL 2000 database with only an MDF file

I have an old server with a defunct evaluation version of SQL 2000 on it (from 2006), and two databases which were sitting on it.
For some unknown reason, the LDF log files are missing. Presumed deleted.
I have the mdf files (and in one case an ndf file too) for the databases which used to exist on that server, and I am trying to get them up and running on another SQL 2000 box I have sitting around.
sp_attach_db complains that the logfile is missing, and will not attach the database. Attempts to fool it by using a logfile from a database with the same name failed miserably. sp_attach_single_file_db will not work either. The mdf files have obviously not been cleanly detached.
How do I get the databases attached and readable?
I found this answer, which worked with my SQL 2000 machines:
How to attach a database with a non-cleanly detached MDF file.
Step 1: Make a new database with same name, and which uses the same files as the old one on the new server.
Step 2: Stop SQL server, and move your mdf files (and any ndf files you have) over the top of the new ones you just created. Delete any log files.
Step 3: Start SQL and run this to put the DB in emergency mode.
sp_configure 'allow updates', 1
go
reconfigure with override
GO
update sysdatabases set status = 32768 where name = 'TestDB'
go
sp_configure 'allow updates', 0
go
reconfigure with override
GO
Step 4: Restart SQL server and observe that the DB is successfully in emergency mode.
Step 5: Run this undocumented dbcc option to rebuild the log file (in the correct place)
DBCC REBUILD_LOG(TestDB,'D:\SQL_Log\TestDB_Log.LDF')
Step 6: You might need to reset the status. Even if you don't, it won't do any harm to do so.
exec sp_resetstatus TestDB
Step 7: Stop and start SQL to see your newly restored database.
In Enterprise Manager, right-click the server and choose Attach Database. Select the MDF file and click Ok. It will then ask you if you want to create a new log file or not. Say Yes.

Resources