22,209
社区成员
发帖
与我相关
我的任务
分享
declare @dbname sysname
declare @servername sysname
SET @servername=CONVERT(nvarchar(128),SERVERPROPERTY(N'ServerName'))
SELECT @dbname=DB_NAME()
--创建作业
exec msdb..sp_add_job
@job_name='job_autobackupdb', --作业名称
@enabled=@isRun --是否可用
EXEC msdb..sp_add_jobserver @job_name = 'job_autobackupdb',
@server_name = @servername
--创建作业步骤
exec msdb..sp_add_jobstep
@job_name='job_autobackupdb',
@step_name='数据处理',
@database_name=@dbname,
@command=@backupSql,
@retry_attempts=5,--重试次数
@retry_interval=5--重试间隔
--创建调度
declare @ftype int
declare @fInterval int --运行周期
declare @ffactor int --作业的两次计划执行之间的间隔周数或月数
set @ftype=case @type when '每天' then 4
when '每周' then 8
when '每月' then 16 end
--运行周期
if @type='每周'
begin
select @fInterval=case @runs when '星期天' then 1
when '星期一' then 2
when '星期二' then 4
when '星期三' then 8
when '星期四' then 16
when '星期五' then 32
when '星期六' then 64
end
end
if @type='每月'
begin
set @fInterval=@runs
end
if @type='每天'
begin
set @fInterval=1
end
set @ffactor=case @type when '每天' then 0 else 1 end
exec msdb..sp_add_jobschedule @job_name='job_autobackupdb',
@name='时间安排',
@freq_type=@ftype, --4每天,8每周,16每月
@freq_interval=@fInterval, --重复执行次数
@freq_subday_type=1, --是否重复执行[按指定时间执行]
@freq_subday_interval=1, --相当于每日重复周期[一般不用]
@freq_recurrence_factor=@ffactor, --作业的两次计划执行之间的间隔周数或月数
@active_start_time=@backupTime --执行时间,在前台要转化成6位字符类似'120000'