Problem
You have to fix a corrupt SQL Server msdb database and to make things worse,
there is no recent database backup. If you run setup with the option
REBUILDDATABASE you will also rebuild the master database and lose all the
logins unless you backup and restore the master database. Is this the best
option? Keep reading and you will see that there is another way to rebuild the
msdb database without compromising the master database.
Solution
Dealing with corruption of system databases is one of the most feared situations
1 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
for SQL Server DBAs. Luckily, there are a few different options to fix the corrupt
SQL Server instance to get back up and running. You can get a list of those
methods in John Grover's tip on How to recover a suspect msdb database in
SQL Server. The last method John proposes in his tip is to "Use a SQL Server
Template MSDB Database", but this option has two paths depending on what
SQL Server version you are working with. If you have an instance of SQL Server
2008 or later, you can copy the database template in the Binn\Templates
subfolder of the instance root directory. But if you are working with a previous
version then you have to use the method that I will expose in this tip.
The possible solution to the previous scenario relies on the Install subfolder of
SQL Server instance root directory, which is usually C:\Program Files\Microsoft
SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Install. There is a file in that
folder named instmsdb.sql which is the msdb creation script. Let's see how we
can use it.
The first step is to stop the SQL Server Instance and all the dependent services
2 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
like Integration Services, Analysis Services, Reporting Services and any other
application that use SQL Server. You can use any administrative tool for that
purpose like the Services Manager. The screen shot below shows a command
window with the commands (i.e. NET STOP) I have used to stop the SQL
Server services.
Now we are going to start the SQL Server service with the following parameters.
The /T3608 parameter enables the trace flag 3608 to prevent the msdb
database from starting up. Notice that the "T" letter is uppercase; most of the
startup flags are specified with an uppercase "T", there are some others that use
a lowercase "t", but are usually used by Microsoft technical support only. The
"/f" parameter is to start SQL Server service with its minimal configuration and
3 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
in single user mode; we want to be the only ones to access the instance. The
last parameter /m"SQLCMD" tells the instance to only accept connections for
an application that identifies as SQLCMD. That's to avoid other applications to
connect to the instance.
After the instance starts up, we open a Command window and change the folder
to the install folder of the instance root directory and from there we connect to it
with the SQLCMD application and detach the msdb database.
The –E parameter is used to establish a trusted connection, you can use a SQL
Server login like sa if you want to. The –Q parameter executes the quoted
commands and exits.
Now you can stop the instance and start it up again normally, I mean without any
special consideration.
The next step is to rename the msdb database files; otherwise the next step will
fail. By default these files are in the DATA sub-folder for the SQL Server
instance.
If you try to connect to the instance using SQL Server Management Studio you
will get an error message, but you can still use SQL Server Management Studio
by selecting New\Database Engine Query, like on the next image.
4 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
Now to recreate the msdb database you can use the next SQLCMD command.
Also you can execute this script by dragging the instmsdb.sql file into SQL
Server Management Studio. The next image is a screen capture showing the
execution.
5 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
/**************************************************************/
/* SYSJOBS */
6 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
/**************************************************************/
7 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
In contraposition, the next script also extracted from instmsdb.sql script creates
the sp_agent_start_job stored procedure. Notice that before the CREATE
PROCEDURE there is an IF block that drops the sp_agent_start_job stored
procedure if it exists.
PRINT ''
PRINT 'Creating procedure sp_agent_start_job...'
IF (NOT OBJECT_ID(N'dbo.sp_agent_start_job', 'P') IS NULL)
DROP PROCEDURE dbo.sp_agent_start_job
GO
The best part of this fix is that you don't have to stop the instance and start it
with a trace flag in order to execute the instmsdb.sql script. The next three
images are screen captures that I took showing the contents of the sysjobs table
before executing instmsdb.sql script, the script execution and the contents of the
sysjobs table after executing instmsdb.sql script.
8 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
9 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
In this final screenshot after instmsdb.sql has been run, we can see that job
SampleJob still exists. So all of the data stays intact after running instmsdb.sql
against an existing msdb database.
10 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
Next Steps
The following tip will go through the basics of system databases: SQL Server
System Databases.
To learn about how to safeguard system databases take a look at Best
Practices Backup System Databases in SQL Server.
This tip will show you step by step the process of Restoring SQL Server
system databases msdb and model.
The following tip shows other ways to recover the msdb database: How to
recover a suspect msdb database in SQL Server..
Check out System Databases Tips Category.
11 of 12 12/8/2017, 5:57 PM
How to Fix a Corrupt MSDB SQL Server Database https://www.mssqltips.com/sqlservertip/3658/how-to-fix-a-corrupt-msdb...
Related Resources
More SQL Server DBA Tips...
12 of 12 12/8/2017, 5:57 PM