Reputation: 1025
How can I attach a database without an LDF file in SQL Server?
Upvotes: 9
Views: 40600
Reputation: 1
CREATE DATABASE TESTDB ON (FILENAME = 'C:\MSSQL\DATA\TESTDB.mdf') For ATTACH_FORCE_REBUILD_LOG
Upvotes: 0
Reputation: 2310
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.
Upvotes: 6
Reputation: 1602
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:
Upvotes: 8
Reputation:
Try this steps through SQL Server Management Studio
Upvotes: 3
Reputation: 31
EXEC sp_attach_single_file_db @dbname = 'DBNAME',@physname = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBNAME_Data.mdf';
I tried and it worked... Hopefully this helps.
Upvotes: 3
Reputation: 23289
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):
Upvotes: 3
Reputation: 9098
You can "just do it" it'll throw a warning that it couldn't find the .ldf, but it will still attach the db.
Upvotes: 1
Reputation: 23236
You can use sp_attach_single_file_db to attach a database which is missing it's log file.
Upvotes: 9