Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all SQL Agent Jobs that call a given stored-proc

I'm in SQL 2008/R2. I want to run a query to see if there is a SQL Agent job calling a specified stored proc (there are too many to inspect manually).

like image 282
NealWalters Avatar asked Aug 07 '13 14:08

NealWalters


People also ask

Where is SQL Agent job history stored?

SQL Server stores job information in the MSDB system tables. The table that stores historical job executions is msdb. dbo. sysjobhistory and the date/time information as well as the duration of the job execution is stored a little different than what you will see in most system tables.

How do I get the script of all jobs in SQL Server?

To script all jobs, just open the 'Object Explorer Details' from the View menu in SSMS, or press the F7 key. Click on any job in the Object Explorer window and a list of all the agent jobs appears in the 'Object Explorer Details' window.

How do you check where a stored procedure is called?

Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure. View the list of objects on which the procedure depends.


1 Answers

Here is a query that will give you that and more (look at the WHERE clause for the stored proc name):

SELECT     [sJOB].[job_id] AS [JobID]     , [sJOB].[name] AS [JobName]     , [sJSTP].[step_uid] AS [StepID]     , [sJSTP].[step_id] AS [StepNo]     , [sJSTP].[step_name] AS [StepName]     , CASE [sJSTP].[subsystem]         WHEN 'ActiveScripting' THEN 'ActiveX Script'         WHEN 'CmdExec' THEN 'Operating system (CmdExec)'         WHEN 'PowerShell' THEN 'PowerShell'         WHEN 'Distribution' THEN 'Replication Distributor'         WHEN 'Merge' THEN 'Replication Merge'         WHEN 'QueueReader' THEN 'Replication Queue Reader'         WHEN 'Snapshot' THEN 'Replication Snapshot'         WHEN 'LogReader' THEN 'Replication Transaction-Log Reader'         WHEN 'ANALYSISCOMMAND' THEN 'SQL Server Analysis Services Command'         WHEN 'ANALYSISQUERY' THEN 'SQL Server Analysis Services Query'         WHEN 'SSIS' THEN 'SQL Server Integration Services Package'         WHEN 'TSQL' THEN 'Transact-SQL script (T-SQL)'         ELSE sJSTP.subsystem       END AS [StepType]     , [sPROX].[name] AS [RunAs]     , [sJSTP].[database_name] AS [Database]     , [sJSTP].[command] AS [ExecutableCommand]     , CASE [sJSTP].[on_success_action]         WHEN 1 THEN 'Quit the job reporting success'         WHEN 2 THEN 'Quit the job reporting failure'         WHEN 3 THEN 'Go to the next step'         WHEN 4 THEN 'Go to Step: '                      + QUOTENAME(CAST([sJSTP].[on_success_step_id] AS VARCHAR(3)))                      + ' '                      + [sOSSTP].[step_name]       END AS [OnSuccessAction]     , [sJSTP].[retry_attempts] AS [RetryAttempts]     , [sJSTP].[retry_interval] AS [RetryInterval (Minutes)]     , CASE [sJSTP].[on_fail_action]         WHEN 1 THEN 'Quit the job reporting success'         WHEN 2 THEN 'Quit the job reporting failure'         WHEN 3 THEN 'Go to the next step'         WHEN 4 THEN 'Go to Step: '                      + QUOTENAME(CAST([sJSTP].[on_fail_step_id] AS VARCHAR(3)))                      + ' '                      + [sOFSTP].[step_name]       END AS [OnFailureAction] FROM     [msdb].[dbo].[sysjobsteps] AS [sJSTP]     INNER JOIN [msdb].[dbo].[sysjobs] AS [sJOB]         ON [sJSTP].[job_id] = [sJOB].[job_id]     LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOSSTP]         ON [sJSTP].[job_id] = [sOSSTP].[job_id]         AND [sJSTP].[on_success_step_id] = [sOSSTP].[step_id]     LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOFSTP]         ON [sJSTP].[job_id] = [sOFSTP].[job_id]         AND [sJSTP].[on_fail_step_id] = [sOFSTP].[step_id]     LEFT JOIN [msdb].[dbo].[sysproxies] AS [sPROX]         ON [sJSTP].[proxy_id] = [sPROX].[proxy_id] WHERE [sJSTP].[command] LIKE '%MyStoredProc%' ORDER BY [JobName], [StepNo] 

Credit should go to the article Querying SQL Server Agent Job Information by Dattatrey Sindol for most of the above query.

like image 62
Michael Goldshteyn Avatar answered Oct 16 '22 08:10

Michael Goldshteyn