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:
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):
Rename existing .mdf file to .mdf_old
Create a new database with same .mdf and .ldf file as old one.
Stop the sql server
Rename .mdf and .ldf files of the new db to .mdf_old and .ldf_old
Rename .mdf_old to .mdf
Start sql server
You should see db in suspect mode
Change the database context to Master and allow updates to system tables
Set the database in Emergency (bypass recovery) mode.
Stop and restart SQL server.
Rebuild the log.
Set the database in single-user mode and run DBCC CHECKDB to validate physical consistency.
9条答案
按热度按时间ruarlubt1#
You can use sp_attach_single_file_db to attach a database which is missing it's log file.
11dmarpk2#
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:
vq8itlhq3#
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)
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
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
Method-3
If you database has only one log file missig you can use this
Further you can read in BOOKs Online to get more information.
kxxlusnw4#
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):
aamkag615#
Try this steps through SQL Server Management Studio
iq3niunx6#
I tried and it worked... Hopefully this helps.
a11xaf1n7#
You can "just do it" it'll throw a warning that it couldn't find the .ldf, but it will still attach the db.
4smxwvx58#
If you run into problems, verify that the mdf file is not read-only.
qhhrdooz9#
CREATE DATABASE TESTDB ON (FILENAME = 'C:\MSSQL\DATA\TESTDB.mdf') For ATTACH_FORCE_REBUILD_LOG