构建此Proc,它将检查作业是否在一台服务器上运行,并将在另一台服务器上启动其他作业。 我在这里遇到的问题是:我无法以这种方式加入 INNER JOIN @ parameterServerName.msdb.dbo.sysjobs 有没有一种方法可以让我加入,而不是写一堆' if'每个可能的输入的陈述。
CREATE Proc [dbo].[JobCheckStarter]
@JobToCheck sysname
,@CheckServer varchar(128)
,@JobToStart sysname
,@StartServer varchar(128)
AS
Begin
SET NOCOUNT ON
-- Create a temporary table to hold job info
CREATE TABLE #JobCheck
(
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
)
CREATE TABLE #JobStart
(
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
)
IF @CheckServer = 'serverOne'
Begin
INSERT INTO #JobCheck
EXEC serverOne.master.dbo.xp_sqlagent_enum_jobs 1,dbo
END
Else
BEGIN
INSERT INTO #JobCheck
EXEC serverTwo.master.dbo.xp_sqlagent_enum_jobs 1,dbo
End
If @StartServer = 'serverOne'
Begin
INSERT INTO #JobStart
EXEC serverOne.master.dbo.xp_sqlagent_enum_jobs 1,dbo
END
Else
BEGIN
INSERT INTO #JobStart
EXEC serverTwo.master.dbo.xp_sqlagent_enum_jobs 1,dbo
End
if exists (
SELECT Running
FROM #JobCheck J INNER JOIN @CheckServer.msdb.dbo.sysjobs SJ ON
J.job_ID = SJ.job_ID
WHERE [name] = @JobToCheck and J.running = 0
)
Begin
if exists (
SELECT Running,*
FROM #JobStart J INNER JOIN @StartServer.msdb.dbo.sysjobs SJ ON
J.job_ID = SJ.job_ID
WHERE [name] = @JobToStart and J.running = 0
)
BEGIN
PRINT 'Starting job ''' + @JobToStart + '''';
EXEC @StartServer.msdb.dbo.sp_start_job @JobToStart;
END
Else
BEGIN
PRINT 'Cannot Start ''' + @JobToStart + ''' Because it is Already Running ';
END
END
ELSE
BEGIN
PRINT 'Job ''' + @JobToCheck + ''' is Running ';
END
Drop TABLE #Job
END
GO
建立一个动态的工作,感谢所有帮助过的人:D
Create Proc [dbo].[JobCheckStarter]
@JobToCheck Varchar(250)
,@CheckServer varchar(128)
,@JobToStart Varchar(250)
,@StartServer varchar(128)
AS
Begin
SET NOCOUNT ON
Declare @Sql nVarchar(max)
Declare @IFSql nvarchar(max)
--Declare @JobToCheck varchar(200)
--Declare @CheckServer varchar(200)
--Declare @JobToStart varchar(200)
--Declare @StartServer varchar(200)
--Set @JobToCheck = ''
--Set @CheckServer =''
--Set @JobToStart = ''
--Set @StartServer =''
Set @Sql = '
CREATE TABLE #JobCheck
(
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
)
CREATE TABLE #JobStart
(
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
)
INSERT INTO #JobCheck
EXEC '+@CheckServer+'.master.dbo.xp_sqlagent_enum_jobs 1,dbo;
INSERT INTO #JobStart
EXEC '+@StartServer+'.master.dbo.xp_sqlagent_enum_jobs 1,dbo;
'
Set @ifSQL = 'if exists (
SELECT Running
FROM #JobCheck J INNER JOIN '+ @CheckServer+ '.msdb.dbo.sysjobs SJ ON
J.job_ID = SJ.job_ID
WHERE SJ.name = '+'''' +@JobToCheck+ '''' +' and J.running = 0
)
Begin
if exists (
SELECT Running
FROM #JobStart J INNER JOIN '+@StartServer+ '.msdb.dbo.sysjobs SJ ON
J.job_ID = SJ.job_ID
WHERE SJ.name = ' +''''+@JobToStart+''''+' and J.running = 0
)'+'
BEGIN
PRINT '+ '''Starting job ''' +'+'+ ''''+@JobToStart+''''+';'+
'
Exec '+ @StartServer+'.msdb.dbo.sp_start_job '+''''+ @JobToStart +''''
+'
END
Else
BEGIN
PRINT '+'''Cannot Start ''' +'+'+ ''''+@JobToStart+'''' +'+'+ ''' Because it is Already Running '''
+'
END
END
ELSE
BEGIN
PRINT '+ '''Job ''' +'+'+ ''''+@JobToCheck+'''' +'+'+ ''' is Running ''' +'
END '
Set @Sql= @Sql+@IFSql
--print @sql
--EXECUTE sp_executesql @Sql
EXEC sp_executesql @Sql, N' @JobToCheck Varchar(250),@CheckServer varchar(128),@JobToStart Varchar(250),@StartServer varchar(128)',@JobToCheck ,@CheckServer,@JobToStart ,@StartServer
END
GO