跟踪得到如下语句,但在我机上不能再次执行了,可能是由于唯一性约束的原因
你看看在你那边能不能执行后生成一个维护计划出来,如果可以,那这个思路是没有问题的:
应该也可以简化一下它:
use [COMPANY]
go
SELECT DATABASEPROPERTYEX(N'COMPANY', N'RECOVERY')
go
use [master]
go
EXECUTE master.dbo.xp_get_tape_devices
go
set noexec off set parseonly off
go
xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE/Microsoft/MSSQLServer/MSSQLServer', N'BackupDirectory'
go
EXECUTE master.dbo.xp_fileexist N'X:/BACKUP'
go
set noexec off set parseonly off
go
DECLARE @retval nvarchar(255) EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE/Microsoft/MSSQLServer/SQLServerAgent', N'ErrorLogFile', @param = @retval OUTPUT SELECT @retval
go
set noexec off set parseonly off
go
SELECT plan_name FROM msdb.dbo.sysdbmaintplans WHERE plan_name LIKE N'数据库维护计划%'
go
set noexec off set parseonly off
go
SELECT plan_name FROM msdb.dbo.sysdbmaintplans WHERE plan_name = N'我的维护计划'
go
set noexec off set parseonly off
go
DECLARE @PlanID nchar(36)
SELECT @PlanID = NEWID()
INSERT msdb.dbo.sysdbmaintplans (plan_id, plan_name, max_history_rows, remote_history_server, max_remote_history_rows)
VALUES (@PlanID, N'我的维护计划', 1000, N'', 0)
SELECT @PlanID
go
set noexec off set parseonly off
go
DECLARE @PlanID char(36)
SELECT @PlanID = '146395E2-5C7C-484C-B69B-5E53B37E54E8'
SELECT plan_name
FROM msdb.dbo.sysdbmaintplans
WHERE plan_id = @PlanID
SELECT database_name
FROM msdb.dbo.sysdbmaintplan_databases
WHERE plan_id = @PlanID
SELECT sj.command
FROM msdb.dbo.sysjobsteps sj,
msdb.dbo.sysdbmaintplan_jobs sp
WHERE sp.plan_id = @PlanID AND
sj.job_id = sp.job_id
SELECT DISTINCT sj.server_id, st.server_name
FROM msdb.dbo.sysdbmaintplan_jobs sp,
msdb.dbo.sysjobservers sj
LEFT OUTER JOIN
msdb.dbo.systargetservers st
ON sj.server_id = st.server_id
WHERE sp.plan_id = @PlanID AND
sj.job_id = sp.job_id
go
set noexec off set parseonly off
go
BEGIN TRANSACTION
DECLARE @ReturnCode INT
DECLARE @PlanID nchar(36)
DECLARE @JobID0 nchar(36)
DECLARE @JobID1 nchar(36)
DECLARE @JobID2 nchar(36)
DECLARE @JobID3 nchar(36)
DECLARE @JobID4 nchar(36)
DECLARE @JobID5 nchar(36)
DECLARE @JobID6 nchar(36)
DECLARE @JobID7 nchar(36)
DECLARE @JobIDD nchar(36)
SELECT @PlanID = N'146395E2-5C7C-484C-B69B-5E53B37E54E8'
EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID4 OUTPUT, @job_name = N'DB 维护计划“我的维护计划”的 DB 备份作业。', @enabled = 1, @category_id = 3
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID4, @step_id = 1, @step_name = N'第 1 步', @subsystem = N'TSQL', @on_success_action = 1, @on_fail_action = 2, @command = N'EXECUTE master.dbo.xp_sqlmaint N''-PlanID 146395E2-5C7C-484C-B69B-5E53B37E54E8 -VrfyBackup -BkUpMedia DISK -BkUpDB "X:/BACKUP" -CrBkSubDir -BkExt "BAK"''', @flags = 4
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID4, @name = N'第 1 调度', @enabled = 1, @freq_type = 8, @active_start_date = 0, @active_start_time = 20000, @freq_interval = 1, @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 = @JobID4, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
SELECT @JobID0, @JobID1, @JobID2, @JobID3, @JobID4, @JobID5, @JobID6, @JobID7
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
go
set noexec off set parseonly off
go
DECLARE @PlanID nchar(36)
SELECT @PlanID = N'146395E2-5C7C-484C-B69B-5E53B37E54E8'
DECLARE @ReturnCode INT
BEGIN TRANSACTION
IF (SELECT COUNT(*) FROM msdb.dbo.sysdbmaintplan_databases WHERE plan_id = @PlanID AND database_name = N'COMPANY') < 1
INSERT msdb.dbo.sysdbmaintplan_databases (plan_id, database_name) VALUES (@PlanID, N'COMPANY')
DELETE FROM msdb.dbo.sysdbmaintplan_jobs WHERE plan_id = @PlanID
INSERT msdb.dbo.sysdbmaintplan_jobs VALUES (@PlanID, N'59270252-EA2E-4428-80B7-A7CD66C70E81')
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
go
set noexec off set parseonly off
go
exec msdb..sp_help_job
go
DECLARE @PlanID char(36)
SELECT @PlanID = '146395E2-5C7C-484C-B69B-5E53B37E54E8'
SELECT plan_name
FROM msdb.dbo.sysdbmaintplans
WHERE plan_id = @PlanID
SELECT database_name
FROM msdb.dbo.sysdbmaintplan_databases
WHERE plan_id = @PlanID
SELECT sj.command
FROM msdb.dbo.sysjobsteps sj,
msdb.dbo.sysdbmaintplan_jobs sp
WHERE sp.plan_id = @PlanID AND
sj.job_id = sp.job_id
SELECT DISTINCT sj.server_id, st.server_name
FROM msdb.dbo.sysdbmaintplan_jobs sp,
msdb.dbo.sysjobservers sj
LEFT OUTER JOIN
msdb.dbo.systargetservers st
ON sj.server_id = st.server_id
WHERE sp.plan_id = @PlanID AND
sj.job_id = sp.job_id
go
set noexec off set parseonly off
go