Sql-Server

如何更改 @schedule_uid=N’????’ 的數量重新創建 SQL Server 代理作業?

  • July 25, 2018

我在 SQL Server 代理作業中有一個 SQL Server 2005 作業,我需要將其移動/轉移到另一個更新的 SQL Server 2008 R2 實例。我能夠編寫腳本,然後將其用作升級後的開發人員中的新查詢視窗。伺服器,似乎(據我所知)我必須對此腳本進行的唯一更改是將其指向正確的伺服器、文件路徑並為其獲取一個新的@schedule_uid 編號,但不確定如何分配該編號.

  • 該作業是為 SSIS 包建構的(在文件路徑中指向它)

感謝您的任何建議

腳本程式碼為:

USE [msdb]
GO

/****** Object:  Job [eLFCopy]    Script Date: 12/19/2013 11:30:57 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 12/19/2013 11:30:57 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[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'eLFCopy', 
       @enabled=1, 
       @notify_level_eventlog=0, 
       @notify_level_email=0, 
       @notify_level_netsend=0, 
       @notify_level_page=0, 
       @delete_level=0, 
       @description=N'This job copies eLF files & folders from stage locations to active directories.', 
       @category_name=N'[Uncategorized (Local)]', 
       @owner_login_name=N'BIO\WDDocManagement', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [RunPackage]    Script Date: 12/19/2013 11:30:57 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'RunPackage', 
       @step_id=1, 
       @cmdexec_success_code=0, 
       @on_success_action=1, 
       @on_success_step_id=0, 
       @on_fail_action=2, 
       @on_fail_step_id=0, 
       @retry_attempts=0, 
       @retry_interval=0, 
       @os_run_priority=0, @subsystem=N'SSIS', 
       @command=N'/FILE "C:\SSIS\Packages\elfcopy.dtsx" /CONNECTION "BRORPORAP1.WDDocumentManagement";"Data Source=BRORPORAP1;Initial Catalog=WDDocumentManagement;Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;" /CONNECTION RCSMTP;"SmtpServer=brutmurex3.bio.ri.redcross.net;UseWindowsAuthentication=True;EnableSsl=False;" /MAXCONCURRENT " -1 " /CHECKPOINTING OFF /REPORTING E', 
       @database_name=N'master', 
       @flags=0
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_jobschedule @job_id=@jobId, @name=N'WeeklySchedule', 
       @enabled=1, 
       @freq_type=8, 
       @freq_interval=2, 
       @freq_subday_type=1, 
       @freq_subday_interval=0, 
       @freq_relative_interval=0, 
       @freq_recurrence_factor=1, 
       @active_start_date=20110701, 
       @active_end_date=99991231, 
       @active_start_time=100, 
       @active_end_time=235959, 
       @schedule_uid=N'c406cae9-49b7-40dd-9438-cef44160f562'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
   IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

如果您編寫作業腳本,它將編寫一個計劃,包括一個 GUID,如果它在新伺服器上尚不存在,則應該創建該 GUID。例如:

EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule
       @job_id=@jobId, @name=N'syspolicy_purge_history_schedule', 
       @enabled=1, 
       @freq_type=4, 
       @freq_interval=1, 
       @freq_subday_type=1, 
       @freq_subday_interval=0, 
       @freq_relative_interval=0, 
       @freq_recurrence_factor=0, 
       @active_start_date=20080101, 
       @active_end_date=99991231, 
       @active_start_time=20000, 
       @active_end_time=235959, 
       @schedule_uid=N'3706c58c-cad3-481f-a565-2e1aed81ee8e'
  ---------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

只要您尚未遷移使用相同計劃的另一個作業,就可以在新伺服器上使用此編號。

如果你有,並且你得到錯誤,整個事情將被回滾。在這種情況下,您確實應該註釋掉這一部分,並手動為該作業分配相同的計劃。但是,如果您真的很懶惰,您可以只更改 GUID 中的幾個字元。

引用自:https://dba.stackexchange.com/questions/55327