如何查找调用给定存储过程的所有 SQL 代理作业
Posted
技术标签:
【中文标题】如何查找调用给定存储过程的所有 SQL 代理作业【英文标题】:How to find all SQL Agent Jobs that call a given stored-proc 【发布时间】:2013-08-08 22:58:18 【问题描述】:我使用的是 SQL 2008/R2。我想运行一个查询以查看是否有一个 SQL 代理作业调用指定的存储过程(手动检查太多)。
【问题讨论】:
这个页面有所有mssqltips.com/sqlservertip/2561/… 在 SSMS 中安装 RedGate SQL 搜索工具。它是免费的,让很多事情变得轻而易举。 【参考方案1】:这是一个查询,它可以为您提供更多信息(查看存储过程名称的 WHERE
子句):
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]
对于上述大部分查询,应归功于 Dattatrey Sindol 的文章Querying SQL Server Agent Job Information。
【讨论】:
【参考方案2】:你可以使用这个查询 -
SELECT s.step_id,
j.[name],
s.database_name,
s.command
FROM msdb.dbo.sysjobsteps AS s
INNER JOIN msdb.dbo.sysjobs AS j ON s.job_id = j.job_id
WHERE s.command LIKE '%Stored_procedure%'
【讨论】:
这适用于sql server 2000(不是错字),可以搜索已执行的存储过程【参考方案3】:还有一种方法可以找到特定实例中所有作业调用的所有过程:
SELECT jss.jobname, jss.step_name, p.name FROM sys.procedures p
CROSS apply
(
SELECT j.name AS jobname, js.step_name FROM msdb.dbo.sysjobsteps js
INNER JOIN msdb.dbo.sysjobs j ON js.job_id=j.job_id
WHERE js.command LIKE '%'+p.name+'%'
) jss
【讨论】:
以上是关于如何查找调用给定存储过程的所有 SQL 代理作业的主要内容,如果未能解决你的问题,请参考以下文章
从存储过程执行 SQL Server 代理作业并返回作业结果