Author |
Topic |
CanadaDBA
583 Posts |
Posted - 2007-01-31 : 12:29:00
|
I want to attach a database that one of the developers had detach it. The LDF file was deleted after detach. I tried to attach and rebuild the log file in SQL2005 using the following:USE [master]GOCREATE DATABASE [Test] ON (FILENAME = N'G:\MSSQL\Data\Test.mdf') FOR ATTACH_REBUILD_LOGGO I get the following error:File activation failure. The physical file name "D:\MSSQLDATA\Test_log.LDF" may be incorrect.The log cannot be rebuilt because the database was not cleanly shut down.Msg 1813, Level 16, State 2, Line 1Could not open new database 'Test'. CREATE DATABASE is aborted. What should I do?Thanks,Canada DBA |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-31 : 13:13:16
|
I've always used sp_attach_single_file_db.Tara Kizer |
|
|
CanadaDBA
583 Posts |
Posted - 2007-01-31 : 14:34:12
|
Thanks for the reply. I used the following code and got the same result.USE [master]GOsp_attach_single_file_db N'Test', N'G:\MSSQL\Data\Test.mdf'GO[/code]File activation failure. The physical file name "D:\MSSQLDATA\Test_log.LDF" may be incorrect.The log cannot be rebuilt because the database was not cleanly shut down.Msg 1813, Level 16, State 2, Line 1Could not open new database 'Test'. CREATE DATABASE is aborted.[/code]Canada DBA |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-31 : 14:38:11
|
I guess your MDF file is corrupt then.Tara Kizer |
|
|
CanadaDBA
583 Posts |
Posted - 2007-01-31 : 14:40:58
|
... and there is no way to fix it?quote: Originally posted by tkizer I guess your MDF file is corrupt then.Tara Kizer
Canada DBA |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-31 : 14:42:55
|
Perhaps Microsoft can fix it.Tara Kizer |
|
|
X002548
Not Just a Number
15586 Posts |
|
pareshmotiwala
Constraint Violating Yak Guru
323 Posts |
Posted - 2007-01-31 : 17:30:28
|
I was able to use the sp_attach_single_file without any problems.So it seems what Tara says, about mdf being bad itself may be the problem. |
|
|
MohammedU
Posting Yak Master
145 Posts |
Posted - 2007-02-01 : 01:57:52
|
Last would be undocumented command DBCC REBUILD_LOG but I am not sure it will work in 2005...1. Rename existing .mdf file to .mdf_old2. Create a new database with same .mdf and .ldf file as old one.3. Stop the sql server 4. Rename .mdf and .ldf files of the new db to .mdf_old and .ldf_old5. Rename .mdf_old to .mdf 6. Start sql server7. You should see db in suspect mode.and then follow the following instructions... Note: you can't update system tables in 2005 so you can use alter database command to change to db emergency mode...8. Change the database context to Master and allow updates to systemtables: Use Master Go sp_configure 'allow updates', 1 reconfigure with override Go9. Set the database in Emergency (bypass recovery) mode: -- note the value of the status column for later use select * from sysdatabases where name = '<db_name>' begin tran update sysdatabases set status = 32768 where name = '<db_name>' -- Verify one row is updated before committing commit tranIf you run DBCC REBUILD_LOG without setting the database in Emergencymode, the command does not work. You do not receive an error, but thelog is not rebuilt either.10. Stop and restart SQL server.If you run DBCC REBUILD_LOG without recycling the server, the followingmessage displays:Server: Msg 5023, Level 16, State 2, Line 1 Database must be put inbypass recovery mode to rebuild the log. DBCC execution completed. IfDBCC printed error messages, contact your system administrator.11. The syntax for DBCC REBUILD_LOG is as follows: DBCC REBUILD_LOG('<db_name>','<log_filename>')where <db_name> is the name of the database and <log_filename> is thephysical path to the new log file, not a logical file name. If you donot specify the full path, the new log is created in the Windows NTsystem root directory (by default, this is the Winnt\System32directory).12. Rebuild the log with this code: DBCC TRACEON (3604) DBCC REBUILD_LOG('<db_name>','<log_filename>') GoIf the command is successful, the following message appears:Warning: The log for database '<db_name>' has been rebuilt.Transactional consistency has been lost. DBCC CHECKDB should be run tovalidate physical consistency. Database options will have to be reset,and extra log files may need to be deleted.After the log is successfully rebuilt, the database is placed in DBO UseOnly mode. That is, the status of the database is 2048 irrespective ofwhat the status was previously. You must reset the status usingsp_dboption or through the SEM.13. Set the database in single-user mode and run DBCC CHECKDB to validatephysical consistency: sp_dboption '<db_name>', 'single user', 'true' DBCC CHECKDB('<db_name>') Go begin tran update sysdatabases set status = <prior value> where name ='<db_name>' -- verify one row is updated before committing commit tran Go14. Turn off the updates to system tables by using: sp_configure 'allow updates', 0 reconfigure with override GoWARNING: After verifying the consistency of the database by running DBCCCHECKDB, and fixing any errors, please make sure to check the databasefor logical consistency as well. Because a new log has been built, thetransactions in theold log are lost, hence you must also verify the logical consistency ofthe data as well.MohammedU |
|
|
CanadaDBA
583 Posts |
Posted - 2007-02-01 : 08:57:58
|
Thanks for the complete explanatory post Mohammed!I had to use the following in step 9.-- note status is changed to: 98568Alter database <db_name> SET Emergency I did everything upto step 12: DBCC REBUILD_LOG('<db_name>','<log_filename>')After running the DBCC command:Msg 2526, Level 16, State 3, Line 1Incorrect DBCC statement. Check the documentation for the correct DBCC syntax and options.Note: I am working on my desktop not the server and SQL Server Management Studio Express is installed on my mchine.In the following link it says: The following dbcc commands are now dead and buried from SQL Server 2005 onwards.And REBUILD_LOG is one of them.from http://www.sqlservercentral.com/columnists/jreade/sqlserver2005dbcccommandquickreference.aspCanada DBA |
|
|
CanadaDBA
583 Posts |
Posted - 2007-02-01 : 09:02:56
|
I also tried: CREATE DATABASE MyTest ON PRIMARY (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\MyTest.mdf')LOG ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\MyTest_log.ldf')FOR ATTACH_REBUILD_LOG; Msg 1801, Level 16, State 3, Line 1Database 'R06_USAA_RERUN' already exists.Canada DBA |
|
|
MohammedU
Posting Yak Master
145 Posts |
Posted - 2007-02-01 : 14:11:13
|
That is the reason I said "Last would be undocumented command DBCC REBUILD_LOG but I am not sure it will work in 2005..."MohammedU |
|
|
CanadaDBA
583 Posts |
Posted - 2007-02-02 : 13:48:59
|
quote: Originally posted by MohammedU That is the reason I said "Last would be undocumented command DBCC REBUILD_LOG but I am not sure it will work in 2005..."MohammedU
Canada DBA |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-02-02 : 15:52:37
|
Why not just restore the database from the last full backup?CODO ERGO SUM |
|
|
Varox
Starting Member
1 Post |
Posted - 2007-03-14 : 16:02:33
|
Thank You MohammedU, you made my day (or evening, it is)! :-)Our db server's log disk was out of disk space and I could neither backup (28 GB log) nor detach the database. Then I *ahem* somehow managed to kill the log during detaching.Not the most important database, but still, reinstalling the application it belonged to would have been quite a hassle. I followed your instructions step by step (SQL Server 2000) and voilà: back online again!!Benjamin |
|
|
jorgejordao
Starting Member
1 Post |
Posted - 2007-11-28 : 17:13:34
|
HiI've the same problem, it all goes well to the step nº9, when i try the next step DBCC TRACEON (3604)DBCC REBUILD_LOG('GesPOSData','C:\Programas\Microsoft SQL Server\MSDMSSQL$MSDE\Data\GesPOSData_log.ldf')Goi get the following messageDBCC execution completed. If DBCC printed error messages, contact your system administrator.Msg 5180, Level 22, State 1, Line 2Could not open FCB for invalid file ID 0 in database 'GesPOSData'.Thanks |
|
|
LeenaAmbulkar
Starting Member
1 Post |
Posted - 2008-08-15 : 00:41:05
|
Thank You MohammedU,The detailed procedure given by you helped us recover vital database for a hospital.How do we avoid Log file size increse to a huge size , which basically started the issue.Thanks again, Leena |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-15 : 01:33:37
|
Backup your transaction log on a regular basis. If the data is vital, you should be using FULL recovery model with regular transaction log backups such as every 15 minutes. Shouldn't they have an experienced DBA supporting a vital hospital database?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
|
Borik
Starting Member
2 Posts |
Posted - 2008-11-10 : 13:20:51
|
Ok, that what i did to get it up and running, Steps are From getting Db out of [Suspect] mode due to corrupt Log file, can be applied to DB attaching without Log File...1. Set DB in Emergency Mode. ALTER DATABASE [dbName] SET EMERGENCY2. Script Create DB, this should simplify the process if you have multiple files, groups, etc...3. In a Generated Script Remove Log Entry 4. Add "For Attach_Rebuild_Log"5. Rename Log files to .OLD, check in few locations like Defulat locations, Data file locations...6. Detach DB, ignore the error, it should be gone from Dabases list, if you refresh... EXEC master.dbo.sp_detach_db @dbname = N'dbName'7. Run Scriptp.s. Posting here as it comes up on Google Search, i tough it might be helpful |
|
|
phuongkar
Starting Member
1 Post |
Posted - 2009-07-03 : 05:46:04
|
I found this way on internet but this article has been wrote by language Vietnamese, you can see http://hoclaptrinhweb.com/?page=learn_detail&id=99&CategoryId=65 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
|
Next Page
|