我怎样才能确定工作的状态?
我有一个存储过程,安排一个工作。 这项工作需要很长时间才能完成(大约30到40分钟)。 我需要了解这份工作的状况。 以下细节将帮助我
1)如何查看未来计划的所有工作清单,但尚未开始
2)如何查看正在运行的作业列表以及运行时间
3)如何查看工作是否已经成功完成或由于任何错误而停滞。
您可以尝试使用系统存储过程sp_help_job。 这将返回有关作业,步骤,时间表和服务器的信息。 例如
EXEC msdb.dbo.sp_help_job @Job_name = 'Your Job Name'
SQL联机丛书应该包含大量有关它返回的logging的信息。
要返回有关多个作业的信息,可以尝试查询以下系统表,这些表包含作业中的各种信息
- msdb.dbo.SysJobs
- msdb.dbo.SysJobSteps
- msdb.dbo.SysJobSchedules
- msdb.dbo.SysJobServers
- msdb.dbo.SysJobHistory
他们的名字是不言而喻的(除了SysJobServers,它们持有关于工作上次运行和结果的信息)。
同样,有关这些字段的信息可以在MSDN上find。 例如,查看SysJobs的页面
我想指出的是,这个页面上的T-SQL都不会正常工作,因为它们没有一个join到syssessions表中,只能获得当前会话,因此可能包含误报。
请参阅此处以供参考: 具有空停止date的工作意味着什么?
您也可以通过分析msdb中的sp_help_jobactivity过程来validation这一点。
我意识到这是一个旧的消息,但我发现这个消息只是部分有用的,因为这个问题。
SELECT job.name, job.job_id, job.originating_server, activity.run_requested_date, DATEDIFF( SECOND, activity.run_requested_date, GETDATE() ) as Elapsed FROM msdb.dbo.sysjobs_view job JOIN msdb.dbo.sysjobactivity activity ON job.job_id = activity.job_id JOIN msdb.dbo.syssessions sess ON sess.session_id = activity.session_id JOIN ( SELECT MAX( agent_start_date ) AS max_agent_start_date FROM msdb.dbo.syssessions ) sess_max ON sess.agent_start_date = sess_max.max_agent_start_date WHERE run_requested_date IS NOT NULL AND stop_execution_date IS NULL
这是我用来获得正在运行的工作(主要是我可以杀死可能已经挂起的工作):
SELECT job.Name, job.job_ID ,job.Originating_Server ,activity.run_requested_Date ,datediff(minute, activity.run_requested_Date, getdate()) AS Elapsed FROM msdb.dbo.sysjobs_view job INNER JOIN msdb.dbo.sysjobactivity activity ON (job.job_id = activity.job_id) WHERE run_Requested_date is not null AND stop_execution_date is null AND job.name like 'Your Job Prefix%'
正如Tim所说,MSDN / BOL文档在sysjobsX表的内容上是相当不错的。 只要记住他们是MSDB中的表格。
-- Microsoft SQL Server 2008 Standard Edition: IF EXISTS(SELECT 1 FROM msdb.dbo.sysjobs J JOIN msdb.dbo.sysjobactivity A ON A.job_id=J.job_id WHERE J.name=N'Your Job Name' AND A.run_requested_date IS NOT NULL AND A.stop_execution_date IS NULL ) PRINT 'The job is running!' ELSE PRINT 'The job is not running.'
我们可以通过多种方式查询msdb来获取详细信息。
很less
select job.Name, job.job_ID, job.Originating_Server,activity.run_requested_Date, datediff(minute, activity.run_requested_Date, getdate()) as Elapsed from msdb.dbo.sysjobs_view job inner join msdb.dbo.sysjobactivity activity on (job.job_id = activity.job_id) where run_Requested_date is not null and stop_execution_date is null and job.name like 'Your Job Prefix%'
您尚未指定如何查看这些详细信息。
对于第一眼,我会build议检查服务器pipe理工作室 。
您可以在Jobs下查看SQL Server代理部分中的作业和当前状态。 如果您select了一项工作,则“财产”页面会显示一条指向工作历史的链接,您可以在其中查看开始和结束时间,是否有任何错误,哪个步骤导致错误等等。
您可以指定警报和通知,以便通过电子邮件发送给您,或者在作业成功完成或失败时进行分页。
有一个工作活动监视器,但实际上我从来没有使用它。 你可以试一试。
如果你想通过T-SQL来检查它,那么我不知道你是怎么做到的。
在SSRS中获得作业运行状态报告的有效和正确方法之一便于查看和监视在服务器上运行的作业。 (.rdl文件附加。) http://www.sqllion.com/2011/11/job-running-status-report-in-ssrs/
上面的任务工作,但我已经看到msdb.dbo.sysjobactivity许多loggingrun_Requested_date不为空,stop_execution_date为空 —-和作业当前不在运行。
我build议运行下面的脚本清除所有的虚假条目(确保当时没有工作正在运行)。
SQL2008:
delete activity from msdb.dbo.sysjobs_view job inner join msdb.dbo.sysjobactivity activity on job.job_id = activity.job_id where activity.run_Requested_date is not null and activity.stop_execution_date is null
这是一个古老的问题,但我也有类似的情况,我需要检查SQL Server上的作业状态。 许多人提到了sysjobactivity表,并指出MSDN文档是伟大的。 但是,我还想突出显示“ 作业活动监视器” ,它提供了在您的服务器上定义的所有作业的状态。
我发现最简单的方法是创build一个存储过程。 input“JobName”并打开。
/*----------------------------------------------------------------------------------------------------------- Document Title: usp_getJobStatus Purpose: Finds a Current Jobs Run Status Input Example: EXECUTE usp_getJobStatus 'MyJobName' -------------------------------------------------------------------------------------------------------------*/ IF OBJECT_ID ( 'usp_getJobStatus','P' ) IS NOT NULL DROP PROCEDURE usp_getJobStatus; GO CREATE PROCEDURE usp_getJobStatus @JobName NVARCHAR (1000) AS IF OBJECT_ID('TempDB..#JobResults','U') IS NOT NULL DROP TABLE #JobResults CREATE TABLE #JobResults ( Job_ID UNIQUEIDENTIFIER NOT NULL, Last_Run_Date INT NOT NULL, Last_Run_Time INT NOT NULL, Next_Run_date INT NOT NULL, Next_Run_Time INT NOT NULL, Next_Run_Schedule_ID INT NOT NULL, Requested_to_Run INT NOT NULL, Request_Source INT NOT NULL, Request_Source_id SYSNAME COLLATE Database_Default NULL, Running INT NOT NULL, Current_Step INT NOT NULL, Current_Retry_Attempt INT NOT NULL, Job_State INT NOT NULL ) INSERT #JobResults EXECUTE master.dbo.xp_sqlagent_enum_jobs 1, ''; SELECT job.name AS [Job_Name], ( SELECT MAX(CAST( STUFF(STUFF(CAST(jh.run_date AS VARCHAR),7,0,'-'),5,0,'-') + ' ' + STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') AS DATETIME)) FROM msdb.dbo.sysjobs AS j INNER JOIN msdb.dbo.sysjobhistory AS jh ON jh.job_id = j.job_id AND jh.step_id = 0 WHERE j.[name] LIKE '%' + @JobName + '%' GROUP BY j.[name] ) AS [Last_Completed_DateTime], ( SELECT TOP 1 start_execution_date FROM msdb.dbo.sysjobactivity WHERE job_id = r.job_id ORDER BY start_execution_date DESC ) AS [Job_Start_DateTime], CASE WHEN r.running = 0 THEN CASE WHEN jobInfo.lASt_run_outcome = 0 THEN 'Failed' WHEN jobInfo.lASt_run_outcome = 1 THEN 'Success' WHEN jobInfo.lASt_run_outcome = 3 THEN 'Canceled' ELSE 'Unknown' END WHEN r.job_state = 0 THEN 'Success' WHEN r.job_state = 4 THEN 'Success' WHEN r.job_state = 5 THEN 'Success' WHEN r.job_state = 1 THEN 'In Progress' WHEN r.job_state = 2 THEN 'In Progress' WHEN r.job_state = 3 THEN 'In Progress' WHEN r.job_state = 7 THEN 'In Progress' ELSE 'Unknown' END AS [Run_Status_Description] FROM #JobResults AS r LEFT OUTER JOIN msdb.dbo.sysjobservers AS jobInfo ON r.job_id = jobInfo.job_id INNER JOIN msdb.dbo.sysjobs AS job ON r.job_id = job.job_id WHERE job.[enabled] = 1 AND job.name LIKE '%' + @JobName + '%'
;WITH CTE_JobStatus AS ( SELECT DISTINCT NAME AS [JobName] ,s.step_id ,s.step_name ,CASE WHEN [Enabled] = 1 THEN 'Enabled' ELSE 'Disabled' END [JobStatus] ,CASE WHEN SJH.run_status = 0 THEN 'Failed' WHEN SJH.run_status = 1 THEN 'Succeeded' WHEN SJH.run_status = 2 THEN 'Retry' WHEN SJH.run_status = 3 THEN 'Cancelled' WHEN SJH.run_status = 4 THEN 'In Progress' ELSE 'Unknown' END [JobOutcome] ,CONVERT(VARCHAR(8), sjh.run_date) [RunDate] ,CONVERT(VARCHAR(8), STUFF(STUFF(CONVERT(TIMESTAMP, RIGHT('000000' + CONVERT(VARCHAR(6), sjh.run_time), 6)), 3, 0, ':'), 6, 0, ':')) RunTime ,RANK() OVER ( PARTITION BY s.step_name ORDER BY sjh.run_date DESC ,sjh.run_time DESC ) AS rn ,SJH.run_status FROM msdb..SYSJobs sj INNER JOIN msdb..SYSJobHistory sjh ON sj.job_id = sjh.job_id INNER JOIN msdb.dbo.sysjobsteps s ON sjh.job_id = s.job_id AND sjh.step_id = s.step_id WHERE (sj.NAME LIKE 'JOB NAME') AND sjh.run_date = CONVERT(CHAR, getdate(), 112) ) SELECT * FROM CTE_JobStatus WHERE rn = 1 AND run_status NOT IN (1,4)
我遇到了一个查询MSDB表(也就是上面列出的代码)的服务器上的问题,因为我的一个工作将会运行,但事实并非如此。 有一个系统存储过程返回执行状态,但是不能做一个没有错误的插入exec语句。 里面是另一个系统存储过程,可以使用一个插入exec语句。
INSERT INTO #Job EXEC master.dbo.xp_sqlagent_enum_jobs 1,dbo
并把表格加载到:
CREATE TABLE #Job (job_id UNIQUEIDENTIFIER NOT NULL, last_run_date INT NOT NULL, last_run_time INT NOT NULL, next_run_date INT NOT NULL, next_run_time INT NOT NULL, next_run_schedule_id INT NOT NULL, requested_to_run INT NOT NULL, -- BOOL request_source INT NOT NULL, request_source_id sysname COLLATE database_default NULL, running INT NOT NULL, -- BOOL current_step INT NOT NULL, current_retry_attempt INT NOT NULL, job_state INT NOT NULL)
SELECT sj.name FROM msdb..sysjobactivity aj JOIN msdb..sysjobs sj on sj.job_id = aj.job_id WHERE aj.stop_execution_date IS NULL -- job hasn't stopped running AND aj.start_execution_date IS NOT NULL -- job is currently running AND sj.name = '<your Job Name>' AND NOT EXISTS( -- make sure this is the most recent run select 1 from msdb..sysjobactivity new where new.job_id = aj.job_id and new.start_execution_date > aj.start_execution_date ) ) print 'running'
这将显示上次运行状态/时间,或者如果正在运行,则显示当前运行时间,步骤编号/信息和SPID(如果它具有关联的SPID)。 它还显示启用/禁用,以及作业用户将其转换为NT SID格式的未解决的用户帐户。
CREATE TABLE #list_running_SQL_jobs (job_id UNIQUEIDENTIFIER NOT NULL, last_run_date INT NOT NULL, last_run_time INT NOT NULL, next_run_date INT NOT NULL, next_run_time INT NOT NULL, next_run_schedule_id INT NOT NULL, requested_to_run INT NOT NULL, request_source INT NOT NULL, request_source_id sysname NULL, running INT NOT NULL, current_step INT NOT NULL, current_retry_attempt INT NOT NULL, job_state INT NOT NULL) declare @sqluser nvarchar(128), @is_sysadmin INT select @is_sysadmin = ISNULL(IS_SRVROLEMEMBER(N'sysadmin'), 0) DECLARE read_sysjobs_for_running CURSOR FOR SELECT distinct SUSER_SNAME(owner_sid) FROM msdb.dbo.sysjobs OPEN read_sysjobs_for_running FETCH NEXT FROM read_sysjobs_for_running into @sqluser WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO #list_running_SQL_jobs EXECUTE master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @sqluser FETCH NEXT FROM read_sysjobs_for_running into @sqluser END CLOSE read_sysjobs_for_running DEALLOCATE read_sysjobs_for_running select j.name , 'Enbld'=CASE j.enabled WHEN 0 THEN 'no' ELSE 'YES' END , '#Min'=datediff(minute,a.start_execution_date,isnull(a.stop_execution_date,getdate())) , 'Status'=CASE WHEN a.start_execution_date is not null and a.stop_execution_date is null THEN 'Executing' WHEN h.run_status=0 THEN 'FAILED' WHEN h.run_status=2 THEN 'Retry' WHEN h.run_status=3 THEN 'Canceled' WHEN h.run_status=4 THEN 'InProg' WHEN h.run_status=1 THEN 'Success' ELSE 'Idle' END , r.current_step , spid=p.session_id , owner=isnull(suser_sname(j.owner_sid),'S-'+convert(nvarchar(12),convert(bigint,UNICODE(left(convert(nvarchar(256),j.owner_sid),1)))-convert(bigint,256)*convert(bigint,UNICODE(left(convert(nvarchar(256),j.owner_sid),1))/256)) +'-'+convert(nvarchar(12),UNICODE(right(left(convert(nvarchar(256),j.owner_sid),4),1))/256+convert(bigint,nullif(UNICODE(left(convert(nvarchar(256),j.owner_sid),1))/256,0))-convert(bigint,UNICODE(left(convert(nvarchar(256),j.owner_sid),1))/256)) +isnull('-'+convert(nvarchar(12),convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),5),1)))+convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),6),1)))*convert(bigint,65536)+convert(bigint,nullif(sign(len(convert(nvarchar(256),j.owner_sid))-6),-1))*0),'') +isnull('-'+convert(nvarchar(12),convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),7),1)))+convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),8),1)))*convert(bigint,65536)+convert(bigint,nullif(sign(len(convert(nvarchar(256),j.owner_sid))-8),-1))*0),'') +isnull('-'+convert(nvarchar(12),convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),9),1)))+convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),10),1)))*convert(bigint,65536)+convert(bigint,nullif(sign(len(convert(nvarchar(256),j.owner_sid))-10),-1))*0),'') +isnull('-'+convert(nvarchar(12),convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),11),1)))+convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),12),1)))*convert(bigint,65536)+convert(bigint,nullif(sign(len(convert(nvarchar(256),j.owner_sid))-12),-1))*0),'') +isnull('-'+convert(nvarchar(12),convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),13),1)))+convert(bigint,UNICODE(right(left(convert(nvarchar(256),j.owner_sid),14),1)))*convert(bigint,65536)+convert(bigint,nullif(sign(len(convert(nvarchar(256),j.owner_sid))-14),-1))*0),'') ) --SHOW as NT SID when unresolved , a.start_execution_date, a.stop_execution_date , t.subsystem , t.step_name from msdb.dbo.sysjobs j left outer join (select distinct * from #list_running_SQL_jobs) r on j.job_id=r.job_id left outer join msdb.dbo.sysjobactivity a on j.job_id=a.job_id and a.start_execution_date IS NOT NULL AND --a.stop_execution_date IS NULL and not exists (select * from msdb.dbo.sysjobactivity at where at.job_id = a.job_id and at.start_execution_date > a.start_execution_date) left outer join sys.dm_exec_sessions p on p.program_name like 'SQLAgent%0x%' and j.job_id=substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),7,2)+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),5,2)+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),3,2)+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),1,2)+'-'+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),11,2)+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),9,2)+'-'+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),15,2)+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),13,2)+'-'+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),17,4)+'-'+substring(substring(p.program_name,charindex('0x',p.program_name)+2,32),21,12) left outer join msdb.dbo.sysjobhistory h on j.job_id=h.job_id and h.instance_id=a.job_history_id left outer join msdb.dbo.sysjobsteps t on t.job_id=j.job_id and t.step_id=r.current_step order by 1 drop table #list_running_SQL_jobs