下面这个函数提供类似于java中的String对象中的字符串分割函数的功能,返回一个表,如下访问:
--select * from SplitStr(';','tablename:datadict;dd_name:sunhaisheng;dd_value:this is a test;dd_rmk:huanjing')
create function fn_SplitStr(
@split varchar(10), --分割符号
@datalist varchar(1000)) --要分割的串
returns @result table(strid int identity,strkey varchar(50),strvalue varchar(200))
as
begin
declare @temp varchar(1000),@seg varchar(100),@commapos int,@spos int,@epos int,@param varchar(100),@fvalue varchar(100),@count int
set @temp=@datalist
set @spos=0
select @epos=charindex(@split,@temp,@spos)
--开始串解析
while @epos<>0
begin
set @seg=substring(@temp,@spos,@epos-@spos)
set @epos=@epos+1
set @spos=@epos
select @epos=charindex(@split,@temp,@spos)
set @commapos=charindex(':',@seg,0)
select @param=substring(@seg,0,@commapos),@fvalue=substring(@seg,@commapos+1,len(@seg)-@commapos+1)
insert @result(strkey,strvalue) values(@param,@fvalue)
if @epos=0
begin
select @seg=substring(@temp,@spos,len(@temp)-@spos+1)
set @commapos=charindex(':',@seg,0)
select @param=substring(@seg,0,@commapos),@fvalue=substring(@seg,@commapos+1,len(@seg)-@commapos+1)
insert @result(strkey,strvalue) values(@param,@fvalue)
break
end
end
--结束串解析
return
end
/********************************************************************************************************/
这个过程调用了很多系统存储过程,这些存储过程有很多参数,本过程对这些过程使用了默认的参数。
使用前置条件:
为系统创建备份目录或确定一个已经存在的目录。
有一条由sysadmin固定服务器角色的成员帐户(执行权限默认授予 public 角色)建立的连接。
Sql server Agent已经启动,才可以令计划得以自动执行。
后置条件:
维护计划建立成功,并已经被启用。
Create proc pc_autobackupcfg
@planname varchar(100), --计划名称
@jobname varchar(100), --工作名称
@dbs varchar(100), --要备份的数据库列表,以逗号分隔开
@sdate int, --开始日期
@stime int, --开始时间
@bukuppath varchar(100)=’’, --备份存放的路径,如果不提供则使用系统默认备份路径
@freq_type1 int=4, --何时将执行作业的值
@freq_interval1 int=1 --作业执行的间隔天数
as
/*
本过程自动创建数据库维护计划,默认调度时间为:每天一次,
关于调度时间的详细信息可参看sp_add_jobschedule过程的sql server帮助
*/
declare @errtext varchar(100)
set @errtext=''
set nocount on
if len(ltrim(rtrim(@jobname)))=0
begin
set @jobname=@planname--+'作业'
end
begin transaction
declare @jobid binary(16)
declare @returncode int
select @returncode = 0
if (select count(*) from msdb.dbo.syscategories where name = N'Database Maintenance') < 1
execute msdb.dbo.sp_add_category @name =N'Database Maintenance'
declare @myplan_id uniqueidentifier
--新建数据库维护计划并把计划标识保存到@myplan_id。
select @myplan_id=plan_id from msdb.dbo.sysdbmaintplans where plan_name=@planname
if @myplan_id is not null
exec msdb.dbo.sp_delete_maintenance_plan @myplan_id
execute msdb.dbo.sp_add_maintenance_plan @planname,@myplan_id output
--为计划指定要备份的数据库
--execute msdb.dbo.sp_add_maintenance_plan_db @myplan_id,'OptiXAMS'
declare @db varchar(50)
select * into #db from dbo.fn_SplitStr(',',@dbs)
declare cur cursor for select strvalue from #db
open cur
fetch next from cur into @db
while(@@fetch_status=0)
begin
execute msdb.dbo.sp_add_maintenance_plan_db @myplan_id,@db
fetch next from cur into @db
end
close cur
deallocate cur
-- 删除同名的警报(如果有的话)。
select @jobid = job_id from msdb.dbo.sysjobs
where (name =@jobname)
if (@jobid is not null)
begin
-- 检查此作业是否为多重服务器作业
if (exists (select * from msdb.dbo.sysjobservers
where (job_id = @jobid) and (server_id <> 0)))
begin
-- 已经存在,因而终止脚本
set @errtext='无法导入作业'+@jobname+',因为已经有相同名称的多重服务器作业。'
raiserror (@errtext, 16, 1)
set @errtext=''
goto quitwithrollback
end
else
-- 删除[本地]作业
execute msdb.dbo.sp_delete_job @job_name =@jobname
select @jobid = null
end
begin
-- 添加作业
execute @returncode = msdb.dbo.sp_add_job @job_id = @jobid output ,
@job_name =@jobname,
@owner_login_name = 'sa',
@description = '没有可用的描述。',
@category_name = 'database maintenance',
@enabled = 1,
@notify_level_email = 0,
@notify_level_page = 0,
@notify_level_netsend = 0,
@notify_level_eventlog = 2,
@delete_level= 0
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
-- 添加作业步骤
declare @sql varchar(1000)
if len(ltrim(rtrim(@bukuppath)))>0
set @sql = 'EXECUTE master.dbo.xp_sqlmaint ''-PlanID '+convert(varchar(100),@myplan_id)+' -WriteHistory -VrfyBackup -BkUpMedia DISK -BkUpDB "'+@bukuppath+'" -BkExt "BAK"'''
else
set @sql = 'EXECUTE master.dbo.xp_sqlmaint ''-PlanID '+convert(varchar(100),@myplan_id)+' -WriteHistory -VrfyBackup -BkUpMedia DISK -BkUpDB -UseDefDir -BkExt "BAK"'''
--下面这句是问题代码,问题在于关键字的小写
--set @sql='execute master.dbo.xp_sqlmaint ''-planid '+convert(varchar(100),@myplan_id)+' -writehistory -vrfybackup -bkupmedia disk -bkupdb "d:/databackup" -bkext "bak"'''
execute @returncode = msdb.dbo.sp_add_jobstep @job_id = @jobid,
@step_id = 1,
@step_name = '第 1 步',
@command = @sql,
@database_name = 'master',
@server = '',
@database_user_name = '',
@subsystem = 'tsql',
@cmdexec_success_code = 0,
@flags = 4,
@retry_attempts = 0,
@retry_interval = 0,
@output_file_name = '',
@on_success_step_id = 0,
@on_success_action = 1,
@on_fail_step_id = 0,
@on_fail_action = 2
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
execute @returncode = msdb.dbo.sp_update_job @job_id = @jobid, @start_step_id = 1
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
-- 添加作业调度
execute @returncode = msdb.dbo.sp_add_jobschedule @job_id = @jobid,
@name = '第 1 调度',
@enabled = 1,
@freq_type = @freq_type1,
@active_start_date =@sdate,--20060103
@active_start_time = @stime,--173200,
@freq_interval = @freq_interval1,
@freq_subday_type = 1,
@freq_subday_interval = 0,
@freq_relative_interval = 0,
@freq_recurrence_factor = 1,
@active_end_date = 99991231,
@active_end_time = 235959
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
-- 添加目标服务器
execute @returncode = msdb.dbo.sp_add_jobserver @job_id = @jobid, @server_name = '(local)'
if (@@error <> 0 or @returncode <> 0) goto quitwithrollback
end
--把作业附加到维护计划
execute msdb.dbo.sp_add_maintenance_plan_job @myplan_id, @jobid
commit transaction
set nocount off
goto endsave
quitwithrollback:
if (@@trancount > 0) rollback transaction
endsave:
/*
--测试
declare @active_start_date int,@active_start_time int
set @active_start_date =cast(convert(varchar(10),getdate(),112) as int)
set @active_start_time=cast(replace(convert(varchar(10),dateadd(mi,20,getdate()),108),':','') as int)
--select @active_start_date,@active_start_time
exec pc_autobackupcfg
@planname ='optixams的维护计划100', --计划名称
@jobname='',
@dbs='OptiXAMS,pubs,Northwind',
@sdate=@active_start_date, --开始日期
@stime=@active_start_time, --开始时间
@bukuppath='d:/databackup',
@freq_type1=4, --何时将执行作业的值
@freq_interval1=1 --作业执行的间隔天数
*/