如果有多个步骤,则SQL Server中的JOB脚本具有多个步骤

时间:2017-08-31 11:00:13

标签: sql-server

我正在尝试创建一个select语句,该语句将使用jobname创建作业并创建作业语句。从那个select语句中我应该能够选择create语句,稍后可以通过在另一台服务器上复制和粘贴来创建作业。

我使用以下脚本尝试了这个。

select @@SERVERNAME,sj.name, '
USE [msdb]
GO

BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=''[Uncategorized (Local)]'' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=JOB, @type=LOCAL, @name=''[Uncategorized (Local)]''
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'''+sj.name+''', 
    @enabled='+convert(varchar(20),sj.enabled)+', 
    @notify_level_eventlog='+convert(varchar(20),sj.notify_level_eventlog)+', 
    @notify_level_email='+convert(varchar(20),sj.notify_level_email)+', 
    @notify_level_netsend='+convert(varchar(20),sj.notify_level_netsend)+', 
    @notify_level_page='+convert(varchar(50),sj.notify_level_page)+', 
    @delete_level='+convert(varchar(50),sj.delete_level)+', 
    @description=N'''+convert(varchar(50),sj.description)+''', 
    @category_name=''[Uncategorized (Local)]'', 
    @owner_login_name='''', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback


EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'''+convert(varchar(50),sjs.step_name)+''', 
    @step_id='+convert(varchar(50),sjs.step_id)+', 
    @cmdexec_success_code='+convert(varchar(50),sjs.cmdexec_success_code)+', 
    @on_success_action='+convert(varchar(50),sjs.on_success_action)+', 
    @on_success_step_id='+convert(varchar(50),sjs.on_success_step_id)+', 
    @on_fail_action='+convert(varchar(50),sjs.on_fail_action)+', 
    @on_fail_step_id='+convert(varchar(50),sjs.on_fail_step_id)+', 
    @retry_attempts='+convert(varchar(50),sjs.retry_attempts)+', 
    @retry_interval='+convert(varchar(50),sjs.retry_interval)+', 
    @os_run_priority='+convert(varchar(50),sjs.os_run_priority)+', @subsystem=N'''+convert(varchar(50),sjs.subsystem)+''', 
    @command=N'''+convert(varchar(50),sjs.command)+''', 
    @database_name=N'''+'master'+''', 
    @flags='+convert(varchar(50),sjs.flags)+'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'''+@@SERVERNAME+'''
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO'
from msdb.dbo.sysjobs sj inner join msdb.dbo.sysjobsteps sjs
ON sj.job_id=sjs.job_id
where subsystem='TSQL'

我成功地创造了一份工作。

现在我需要可以生成步骤的脚本,以便多个步骤也包含在上面生成的脚本中。

1 个答案:

答案 0 :(得分:0)

我找到了符合我要求的脚本,它可以使用同一个表中的步骤检索select语句中的所有作业。

select  @@SERVERNAME as PCNAME,sj.job_id,sjs.step_name,sjs.step_id,sj.name,
'
USE [msdb]
GO

BEGIN TRANSACTION
   DECLARE @ReturnCode INT
   SELECT @ReturnCode = 0
   IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=''[Uncategorized (Local)]'' AND category_class=1)
   BEGIN
     EXEC @ReturnCode = msdb.dbo.sp_add_category @class=JOB, @type=LOCAL, @name=''[Uncategorized (Local)]''
     IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
   END

   DECLARE @jobId BINARY(16)
   EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'''+sj.name+''',@enabled='+convert(varchar(20),sj.enabled)+',@notify_level_eventlog='+convert(varchar(20),sj.notify_level_eventlog)+',@notify_level_email='+convert(varchar(20),sj.notify_level_email)+',@notify_level_netsend='+convert(varchar(20),sj.notify_level_netsend)+',@notify_level_page='+convert(varchar(50),sj.notify_level_page)+',@delete_level='+convert(varchar(50),sj.delete_level)+', @description=N'''+convert(varchar(50),sj.description)+''',@category_name=''[Uncategorized (Local)]'', @owner_login_name='''', @job_id = @jobId OUTPUT

   IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

  EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'''+convert(varchar(50),sjs.step_name)+''',@step_id='+convert(varchar(50),sjs.step_id)+',@cmdexec_success_code='+convert(varchar(50),sjs.cmdexec_success_code)+',@on_success_action='+convert(varchar(50),sjs.on_success_action)+',@on_success_step_id='+convert(varchar(50),sjs.on_success_step_id)+',@on_fail_action='+convert(varchar(50),sjs.on_fail_action)+',@on_fail_step_id='+convert(varchar(50),sjs.on_fail_step_id)+',@retry_attempts='+convert(varchar(50),sjs.retry_attempts)+',@retry_interval='+convert(varchar(50),sjs.retry_interval)+',@os_run_priority='+convert(varchar(50),sjs.os_run_priority)+',subsystem=N'''+convert(varchar(50),sjs.subsystem)+''',@command=N'''+convert(varchar(50),sjs.command)+''',@database_name=N'''+'master'+''',@flags='+convert(varchar(50),sjs.flags)+'

IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name 
= N'''+@@SERVERNAME+'''
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO' as script into #temp1
from msdb.dbo.sysjobs sj inner join msdb.dbo.sysjobsteps sjs
ON sj.job_id=sjs.job_id
where subsystem='TSQL'​
and sjs.step_id=1


declare @jobstep as int,@command as varchar(max), @stepid int=2
declare concatenation cursor for
select job_id,step_name,step_id, 
'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, 
@step_name=N'''+step_name+''', 
@step_id='+convert(varchar(20),step_id)+', 
@cmdexec_success_code='+convert(varchar(20),cmdexec_success_code)+',
@on_success_action='+convert(varchar(20),on_success_action)+', 
@on_success_step_id='+convert(varchar(20),on_success_step_id)+', 
@on_fail_action='+convert(varchar(20),on_fail_action)+', 
@on_fail_step_id='+convert(varchar(20),on_fail_step_id)+', 
@retry_attempts='+convert(varchar(20),retry_attempts)+', 
@retry_interval='+convert(varchar(20),retry_interval)+', 
@os_run_priority='+convert(varchar(20),os_run_priority)+', 
@subsystem=N'''+subsystem+''', 
@command=N'''+command+''''', 
@server=N'''+@@SERVERNAME+''''', 
@database_name=N'''+database_name+''', 
@flags='+convert(varchar(20),flags)+', 
@proxy_name=N'''+case when proxy_id is null then CURRENT_USER end+'''' as 
command from msdb.dbo.sysjobsteps
where step_id<>1
open concatenation;

fetch next from concatenation
while @@FETCH_STATUS=0
begin 
update  t 
set t.script=CONCAT(t.script,'   ----------- ',command)
from  #temp1 t inner join msdb.dbo.sysjobsteps sjs
ON t.job_id=sjs.job_id
where t.job_id=sjs.job_id
and sjs.step_id<>1
and sjs.step_id=@stepid

set @stepid=@stepid+1
fetch next from concatenation


end
close concatenation
deallocate concatenation



select * from #temp1
DROP TABLE #temp1