43

Is there a way to find out what jobs are using a certain stored procedure?

Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248

1 Answers1

91

This will capture instances where the procedure is explicitly referenced in the job step:

DECLARE @s nvarchar(255) = N'procedure_name';

SET @s = QUOTENAME(@s, nchar(37));

SELECT job_name = j.name,
       s.step_id,
       s.step_name,
       j.date_created,
       j.date_modified
  FROM msdb.dbo.sysjobs AS j
  INNER JOIN msdb.dbo.sysjobsteps AS s
  ON j.job_id = s.job_id
      WHERE s.command   LIKE @s
         OR j.name      LIKE @s
         OR s.step_name LIKE @s;

If it is called by something else that is called from the job, or the command is constructed with dynamic SQL, this might be a little more difficult to track down. Note also that if your procedure name can also appear naturally in other code, comments, etc. that it may produce false positives.

A much more elaborate and flexible search procedure that I updated last year:

And even better is this module in AMtwo's DBA Database:

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • 3
    Slight modification to this: `DECLARE @Search varchar(255) SET @Search='searchstring' SELECT j.name, s.step_id, s.step_name FROM msdb.dbo.sysjobsteps AS s, msdb.dbo.sysjobs as j WHERE s.job_id = j.job_id AND s.command LIKE '%'+@Search+'%'` – Goner Doug Apr 21 '17 at 20:27