Saturday 4 June 2011

How to Rebuild Sql Server Data base log file ?

DBCC REBUILD_LOG command can be run in SQL to rebuild the log file from the actual database.
The first issue with the DBCC REBUILD_LOG command was that the database needed to exist in MSSQL before this can be run. You can get around this by creating a dummy database as follows:
1. Stop your SQL server.
2. Delete Your LDF file.
3. Start the SQL server – DO NOT try to access the database yet!
Your MDF file is present in MSSQL but since there is no transaction log file SQL will throw a wobbly if you try to access the database. What you need to do next is run a series of SQL commands to set the database to emergency recovery mode. This is done via the SQL Query Analyzer under the Master database.

EXEC sp_configure 'allow updates', 1
RECONFIGURE WITH OVERRIDE
GO
BEGIN TRAN
UPDATE master..sysdatabases
SET status = status | 32768
WHERE name = '<Your Database Name Goes Here>'
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
You now need to restart SQL server. Returning to the SQL Query Analyser run the following command
DBCC REBUILD_LOG('<Your Database Name Goes Here>','C:\filePath\filename.LDF')
ALTER DATABASE <Your Database Name Goes Here> SET MULTI_USER
GO
DBCC CHECKDB ('<Your Database Name Goes Here>')

This will then rebuild the transaction log to the path you specified in the REBUILD_LOG command and your all done!
Again be aware that you may lose some transactional integrity since the LDF file is an integral part of the database. This method can also be used if the LDF file has been accidentally removed.

1 comment: