How can I attach a database without an LDF file in SQL Server?
Once the master database file is repaired, you can use it to attach the SQL database without transaction log-file using either SSMS or executing a query in Transact-SQL.
Attach MDF File Without LDF file by using T-SQL script: You can also run a TSQL Script on SQL Query to restore MDF database in SQL Server and recreate your transaction log file. Where, testdb is the name of your database. Now you can check your database in the database folder.
You can't delete the ldf as long as SQL server is running. If you stop SQL Server and delete the ldf, then you will get into below state: "Recovery pending". This means that recovery failed and it is time to perform a restore.
You can use sp_attach_single_file_db to attach a database which is missing it's log file.
Try to attach it by adding the MDF file to the Attach Databases dialog. You'll note that the dialog will report the missing LDF file. Follow the steps as shown on the picture:
Here are Code Snippets to programaticaly create .ldf files
Following are 3 Methods.
Method -1
In my case I have my Database in DATA folder.
You can get the full path to your Database by right clicking and then going to properties then you can copy the full path to your Database
As In my case path is as follows.
C:\Program Files\Microsoft SQL Server\MSSQL11.DRIBBLEE\MSSQL\DATA
Now here is First Method by using store procedure(sp_attach_single_file_db) and passing it arguments(database name and physical path)
USE [master]
GO
EXECUTE sp_attach_single_file_db
@dbname='AdventureWorksDW_2012',
@physname=N'C:\ProgramFiles\MicrosoftSQLServer\MSSQL11.DRIBBLEE\MSSQL\DATA\AdventureWorksDW2012_Data.mdf'
GO
execute the code you after executing the code go to your database folder where it resides you will see .ldf file created over there.
However you will get following message in your
The physical file name "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\AdventureWorksDW2012_log.ldf" may be incorrect.
New log file 'C:\Program Files\Microsoft SQL Server\MSSQL11.DRIBBLEE\MSSQL\DATA\AdventureWorksDW_2012_log.ldf' was created.
Now you can attach your database and after Attaching the Database right click at your server name in Object Explorer and refresh.
Method-2
IF your database have one or more log files missing you can use following
CREATE DATABASE db_namehere ON
(
FILENAME=N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\db_namehere.mdf')
FOR ATTACH_REBUILD_LOG
GO
Method-3
If you database has only one log file missig you can use this
CREATE DATABASE db_name ON
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TestDb.mdf')
FOR ATTACH
GO
Further you can read in BOOKs Online to get more information.
You can try what is posted here by MohammedU. Basically, what he uses the DBCC REBUILD_LOG command. It will work depending on the version of your server.
Here are the steps (without details):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With