Wednesday, February 25, 2009

SQL SERVER 2000 - Database recovery from an *.MDF file

This is another problem which had bothered me alot. Database log file went corrupt, even though I had a backup of the databases (IMP: always keep a backup), the problem was still in my mind looking for a solution for this. With the help of other fellow engineers in the forums for which I had surfed, I found the way to create the *.ldf file using the *.mdf file. Below I have described how to go about it.

================================================
/*
1) ------ Very Important Step ------ create a new database of the right size and shape with same name of log and data and the file locations, of the corrupted DB.

2) stop sql server in service manager

3) rename the new database.mdf (or delete it if you don't have enough space) -- do not touch the .ldf

4) move back in the old database .mdf file or rename it back again

5) restart sql server

6) it should come up suspect

7) After this run the below query in the master DB

-- Note -- I have used 'ABCDEF' as an example in the below query, replace it with the DB which you want to recover.
*/


EXEC sp_configure 'allow updates', 1
RECONFIGURE WITH OVERRIDE
GO

BEGIN TRAN

UPDATE master..sysdatabases
SET status = status | 32768
WHERE filename = 'E:\Data\ABCDEF_Data.mdf' ------- Changes to be done

IF @@ROWCOUNT = 1
BEGIN
COMMIT TRAN
RAISERROR('emergency mode set', 0, 1)
END
ELSE
BEGIN
ROLLBACK
RAISERROR('unable to set emergency mode', 16, 1)
END

GO

EXEC sp_configure 'allow updates', 0
RECONFIGURE WITH OVERRIDE
GO

-- Restart SQL Server at this point.

DBCC REBUILD_LOG('ABCDEF','E:\Data\ABCDEF_Log.ldf') ------- Changes to be done


/*Perform physical and logical integrity checks at this point.
Bcp data out if your integrity checks demonstrate that problems exist.
*/

ALTER DATABASE ABCDEF SET MULTI_USER ------- Changes to be done
GO


GO
=================================================

well thats it your have your old database back online.

Here (E:\) is my drive in which the data is stored.

No comments:

Post a Comment