MSSQL扩展存储过程 master.dbo.xp_sqlagent_enum_jobs--整理帖
2010-07-21 15:41
483 查看
--> Title : 扩展存储过程 master.dbo.xp_sqlagent_enum_jobs
--> Author : wufeng4552
--> Date : 2010-02-25
/*
要想得到 SQL Server Job 的运行状态,需要用到一个微软未公开的一个扩展存储过程: master.dbo.xp_sqlagent_enum_jobs.
这个扩展存储过程用到一个叫做 “xpstar90.dll” 的动态链接库文件。
*/
/*
xp_sqlagent_enum_jobs <is sysadmin (0 or 1)>, <job owner name> [, <job id>]
我们在调用的时候,@is sysadmin=1,@job owner name 可以随便给个字符串值。 @job id 可有可无。例如,可以这样查询所有的作业状况:
*/
execute master.dbo.xp_sqlagent_enum_jobs 1, 'sa'
--查询特定 Job 的运行状态:
declare @job_id uniqueidentifier
select @job_id = job_id
from msdb.dbo.sysjobs
where [name] = 'your job name'
exec xp_sqlagent_enum_jobs 1, 'sa', @job_id
--判断 SQL Server 作业(Job)是否正在运行的代码。
-- 1. create temp table to save jobs status
create table #job_run_status
(
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
)
-- 2. get jobs status
insert into #job_run_status
execute master.dbo.xp_sqlagent_enum_jobs 1, 'sa'
-- 3. get running jobs
select job_name = j.name
,s.*
from #job_run_status s
inner join msdb.dbo.sysjobs j on s.job_id = j.job_id
where s.running = 1 -- running = 1
--查看Job是否在運行
Declare @Job_ID as UNIQUEIDENTIFIER
select @Job_ID =Job_ID from msdb.dbo.sysjobs where name = 'Jobname'
Exec master..sp_MSget_jobstate @Job_ID
--返回值为 1 - 正在运行
-- 4 - 表示完成(成功或失败)
--查看執行的结果状态
select category = jc.name, category_id = jc.category_id, job_name = j.name,
job_enabled = j.enabled,
last_run_time = cast(js.last_run_date as varchar(10)) + '-' + cast(js.last_run_time as varchar(10)),
last_run_duration = js.last_run_duration,
last_run_status = js.last_run_outcome,
last_run_msg = js.last_outcome_message + cast(nullif(js.last_run_outcome,1) as varchar(2)),
job_created = j.date_created,
job_modified = j.date_modified
from msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobservers js on j.job_id = js.job_id
inner join msdb.dbo.syscategories jc on j.category_id = jc.category_id
where j.enabled = 1 and js.last_run_outcome in (0,1,3,5)
0:Fail 1:Succ 3:Cancel 5:First run and jc.category_id not between 10 and 20
select step_id,
run_status,
run_date As rundate
from msdb.dbo.sysjobhistory a
inner join msdb.dbo.sysjobs b on a.job_id=b.job_id
where b.name='PCDBI'
order by run_date DESC,step_id Asc
/*
作业的执行状态run_status:
0 = 失败
1 = 成功
2 = 重试
3 = 取消
4 = 正在进行
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2010/02/25/5325977.aspx
--> Author : wufeng4552
--> Date : 2010-02-25
/*
要想得到 SQL Server Job 的运行状态,需要用到一个微软未公开的一个扩展存储过程: master.dbo.xp_sqlagent_enum_jobs.
这个扩展存储过程用到一个叫做 “xpstar90.dll” 的动态链接库文件。
*/
/*
xp_sqlagent_enum_jobs <is sysadmin (0 or 1)>, <job owner name> [, <job id>]
我们在调用的时候,@is sysadmin=1,@job owner name 可以随便给个字符串值。 @job id 可有可无。例如,可以这样查询所有的作业状况:
*/
execute master.dbo.xp_sqlagent_enum_jobs 1, 'sa'
--查询特定 Job 的运行状态:
declare @job_id uniqueidentifier
select @job_id = job_id
from msdb.dbo.sysjobs
where [name] = 'your job name'
exec xp_sqlagent_enum_jobs 1, 'sa', @job_id
--判断 SQL Server 作业(Job)是否正在运行的代码。
-- 1. create temp table to save jobs status
create table #job_run_status
(
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
)
-- 2. get jobs status
insert into #job_run_status
execute master.dbo.xp_sqlagent_enum_jobs 1, 'sa'
-- 3. get running jobs
select job_name = j.name
,s.*
from #job_run_status s
inner join msdb.dbo.sysjobs j on s.job_id = j.job_id
where s.running = 1 -- running = 1
--查看Job是否在運行
Declare @Job_ID as UNIQUEIDENTIFIER
select @Job_ID =Job_ID from msdb.dbo.sysjobs where name = 'Jobname'
Exec master..sp_MSget_jobstate @Job_ID
--返回值为 1 - 正在运行
-- 4 - 表示完成(成功或失败)
--查看執行的结果状态
select category = jc.name, category_id = jc.category_id, job_name = j.name,
job_enabled = j.enabled,
last_run_time = cast(js.last_run_date as varchar(10)) + '-' + cast(js.last_run_time as varchar(10)),
last_run_duration = js.last_run_duration,
last_run_status = js.last_run_outcome,
last_run_msg = js.last_outcome_message + cast(nullif(js.last_run_outcome,1) as varchar(2)),
job_created = j.date_created,
job_modified = j.date_modified
from msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobservers js on j.job_id = js.job_id
inner join msdb.dbo.syscategories jc on j.category_id = jc.category_id
where j.enabled = 1 and js.last_run_outcome in (0,1,3,5)
0:Fail 1:Succ 3:Cancel 5:First run and jc.category_id not between 10 and 20
select step_id,
run_status,
run_date As rundate
from msdb.dbo.sysjobhistory a
inner join msdb.dbo.sysjobs b on a.job_id=b.job_id
where b.name='PCDBI'
order by run_date DESC,step_id Asc
/*
作业的执行状态run_status:
0 = 失败
1 = 成功
2 = 重试
3 = 取消
4 = 正在进行
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2010/02/25/5325977.aspx
相关文章推荐
- MSSQL扩展存储过程 master.dbo.xp_sqlagent_enum_jobs--整理帖
- The EXECUTE permission was denied on the object 'xp_sqlagent_enum_jobs', database 'mssqlsystemresour
- 【xp_sqlagent_enum_jobs】取得SQL Server 作业执行状态的存储过程
- 【xp_sqlagent_enum_jobs】取得SQL Server 作业执行状态的存储过程。
- T-SQL系列之MSSQL_未公开的扩展存储过程
- javax.transaction.xa.XAException: java.sql.SQLException: 无法创建 XA 控制连接。错误: 未能找到存储过程 'master..xp_sqljd
- SQL2000未能找到存储过程_master.dbo.xp_regread
- sqlserver 2000 未能找到存储过程 master.dbo.xp_dirtree
- javax.transaction.xa.XAException: java.sql.SQLException: 无法创建 XA 控制连接。错误: 未能找到存储过程 'master..xp_sqljdbc_xa_init'
- 未能找到存储过程 master.dbo.xp_dirtree|错误2812
- SQL Server ->> 调用系统内建扩展存储过程"master.dbo.xp_delete_file"删除过期备份文件
- 未能找到存储过程_master.dbo.xp_regread
- 查询SQL错误日志扩展存储过程xp_readerrorlog
- 未能找到存储过程 master.dbo.xp_dirtree|错误2812
- 查询SQL错误日志扩展存储过程xp_readerrorlog
- SQL Server 阻止了对组件 'Agent XPs' 的 过程 'dbo.sp_set_sqlagent_properties' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。
- Sql server c#扩展存储过程支持并行多线程运算
- SQL语法整理[5]——存储过程
- 请求帮助 SQL 2005 master..xp_cmdshell 死锁问题
- SQL Server数据库里的Management里的SQL Server Agent里的Jobs是个定时运行功能(死亡;历险)