Database in emergency mode The database is in emergency mode and I have tried fixing it. But when I used the statement DBCC CheckDB (TestDB, REPAIR_ALLOW_DATA_LOSS) the log was not rebuilt. The log file is not present now. How can i bring the database online?
Do you have a backup and restore disaster recovery solution in place?  If so, restore the database from the most recent backup.

Assuming you don't have a backup, you may be able to use CREATE DATABASE ... FOR ATTACH_REBUILD_LOG, (see the Microsoft Docs page for details)

The general syntax for the command is:

CREATE DATABASE &lt;db-name&gt;
ON (NAME = '&lt;logical-name&gt;', FILENAME = '&lt;source-to-mdf&gt;')
FOR ATTACH_REBUILD_LOG;


Be aware that if the mdf file is corrupt, you may lose everything.  Ensure you keep a copy of the mdf prior to attempting to attach it in this way.

An example of creating a database, setting it to "emergency" mode, detaching it, then re-attaching it without an existing log file:

CREATE DATABASE AttachTest
ON (NAME = 'sys', FILENAME = 'C:\temp\AttachTest.mdf')
LOG ON (NAME = 'log', FILENAME = 'C:\temp\AttachTest.ldf');

ALTER DATABASE AttachTest SET EMERGENCY;


Detach the database:

EXEC sys.sp_detach_db @dbname = 'AttachTest';


Delete the log file from the filesystem:

EXEC sys.xp_cmdshell 'del "C:\temp\AttachTest.ldf"';


Attach the database, recreating the missing log file:

CREATE DATABASE AttachTest
ON (NAME = 'sys', FILENAME = 'C:\temp\AttachTest.mdf')
FOR ATTACH_REBUILD_LOG


You'll see this message:


  File activation failure. The physical file name "C:\temp\AttachTest.ldf" may be incorrect.
  New log file 'C:\somepath\AttachTest_log.ldf' was created.


Cleanup the test database:

DROP DATABASE AttachTest;