It is indeed a pleasure when we write a timeless article for SQL Server and DBAs read it after many years. In the year 2007, I wrote an article about an error related to Could not open a new database. It has been one of the top viewed blog posts all the time. Recently SQL Server Expert Jeremy Roe has sent me an updated script for a recent version of SQL Server for the same error.
Here is the error message:
Fix: Error Msg 1813, Level 16, State 2, Line 1
Could not open new database ‘yourdatabasename’. CREATE DATABASE is aborted.
Jeremy wrote this script for SQL Server 2016 but we believed it will just work for any version which is SQL Server 2012 and onwards. Let us see his script where he fixes the error related to not opening a database.
Modified step tips:
- Rename the old log file if present like blah_log_OLD just to be safe
- Make sure to rename the DBS carefully to not mix them up or accidentally delete
- Once confirmed everything is working, delete the fake database files and the unutilized older log file if no longer needed
SQL Server logs are corrupted and they need to be rebuilt to make the database operational.
Follow all the steps in order. Replace the yourdatabasename name with real name of your database.
1. Create a new database with same name which you are trying to recover or restore. (In our error message it is yourdatabasename). Make sure the name of the MDF file (primary data file) and LDF files (Log files) same as previous database data and log file.
2. Stop SQL Server. Move original MDF file from older server (or location) to new server (or location) by replacing just created MDF file. Delete the LDF file of new server just created.
3. Start SQL Server. Database will be marked as suspect, which is expected.
4. Make sure system tables of Master database allows to update the values.
USE MASTER GO sp_CONFIGURE 'allow updates', 1 RECONFIGURE WITH OVERRIDE GO
5. Change database mode to emergency mode. The following statement will return the current status of the database
For SQL Server 2005-2008 version:
BEGIN UPDATE sysdatabases SET status = 32768 WHERE name = 'yourdatabasename' COMMIT TRAN
This is replaced in sql2012+:
ALTER DATABASE yourdatabasename SET EMERGENCY;
6. Restart SQL Server (This is must, it is not done SQL Server will through an error)
7. Execute this DBCC command in query window of Management Studio, this will create a new log file. Keep the name of this file same as LDF file just deleted from new server :
DBCC TRACEON (3604) DBCC REBUILD_LOG(yourdatabasename,'c:\yourdatabasename_log.ldf') GO
DBCC TRACEON (3604)
For SQL Server 2005-2008 version:
ALTER DATABASE yourdatabasename REBUILD LOG ON (NAME=yourdatabasename_log,FILENAME='A:\sql_log\yourdatabasename_log.ldf')
DBCC accepts two parameters: the first parameter is the database name and the second parameter is the physical path of the log file. Make sure the path is physical, if you put logical file name it will return an error.
8. Reset the database status using the following command.
9. Turn off the update to system tables of Master database running following script.
USE MASTER GO sp_CONFIGURE 'allow updates', 0 RECONFIGURE WITH OVERRIDE GO
Take DB out of emergency mode and back to MULTI_USER.
ALTER DATABASE yourdatabasename SET MULTI_USER; GO
Well, that’s it. Thanks, Jeremy for an awesome update to the original script.
Reference: Pinal Dave (https://blog.sqlauthority.com)
With SQL 2012, this command return error (step 7)
DBCC Syntax error verify DBCC Options