Where are SQL Jobs Stored? – Interview Question of the Week #293

Question: Where are SQL Server Agent jobs stored? Job definitions are in msdb, with their main records in msdb.dbo.sysjobs. A deleted job is not kept there in an undelete folder.

An open wooden cabinet holds organized rolls of material with one vermilion roll

A reader asked this on my YouTube channel because a job had been deleted accidentally. The database name answers where to look for existing jobs. It does not by itself recover the deleted definition. That second part is why I keep reminding clients to back up the system databases as well as their user databases.

List the jobs that still exist

SELECT job_id, name, enabled, date_created, date_modified
FROM msdb.dbo.sysjobs
ORDER BY name;

Job steps, schedules and execution history are stored in other related msdb tables, such as sysjobsteps, sysjobschedules, sysschedules and sysjobhistory. History describes executions; it is not a substitute for a complete script of the job’s commands and settings.

Recover a definition, not an entire production msdb casually

First look for a saved CREATE script, deployment definition or earlier system-database backup. Recover the earlier msdb state on a separate compatible recovery instance and script the missing job using supported tools. Restoring an old msdb directly over production would also roll back other jobs, schedules and history.

Before recreating it, review its owner, credentials or proxies, commands, target databases and schedule. Keep a recovered job disabled until those details are checked. Do not write directly to SQL Server’s system tables.

TempDB is different: it is recreated at service startup and is not backed up. Its creation time can also give you a quick clue to the last SQL Server service startup, rather than the last Windows restart. That exception does not remove the need for backups of msdb, master and model. Restrict access to msdb because job definitions may contain sensitive operational details.

Here are my related job queries and demonstrations:

The sysjobs reference documents the definition table. The msdb guidance recommends keeping its backup current after job changes.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Scripts, SQL Server, SQL Server Agent
Previous Post
When was Stored Procedure Last Compiled? – Interview Question of the Week #292
Next Post
What is Temp Stored Procedures? – Interview Question of the Week #294

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.