Thursday, December 6, 2007

SQL Error 945 While Creating New Database

I ran across a problem creating a new database on a SQL Server that had recently been through a crash and rebuild. It had been running fine for months, but now whenever you tried to create a new database it would throw a SQL 945 error, referring to space or memory allocation issues and then referencing the data or log file of an unrelated database that had been previously deleted.
After some troubleshooting I came to the conclusion that there must be some orphaned data in the Master database. I first checked sysdatabases, and everything looked fine, 23 databases all in a row. Next I looked at sysaltfiles, and lo and behold there were entries for dbid 24, which did not exist. Since it was the next available dbid in sysdatabases, SQL Server was attempting to create the new database as dbid 24, but the data in sysaltfiles did not jive with it. Long story short, I removed the entries for dbid 24 in sysaltfiles and the problem was fixed.