指定同步處理排程

適用於:SQL ServerAzure SQL 受控執行個體

此主題說明如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 在 SQL Server 中指定同步處理排程。 當您建立訂閱時,可以定義一個同步排程,以控制訂閱的複寫代理程式將於何時執行。 如果不指定排程參數,訂閱將使用預設排程。

訂閱是由散發代理程式 (適用於快照式與異動複寫) 或合併代理程式 (適用於合併式複寫) 同步處理。 代理程式可以繼續執行、視需要執行,或是依照排程執行。

本主題內容

使用 SQL Server Management Studio

在「新增訂閱精靈」的 [同步排程] 頁面中指定同步排程。 如需有關存取這個精靈的詳細資訊,請參閱< Create a Push Subscription >與< Create a Pull Subscription>。

在 [作業排程屬性] 對話方塊中修改同步處理排程,您可從 SQL Server Management Studio 的 [作業] 資料夾和「複寫監視器」的代理程式詳細資料視窗中存取此對話方塊。 如需啟動複寫監視器的詳細資訊,請參閱啟動複寫監視器

如果從 [作業] 資料夾指定排程,請使用下列資料表以決定代理程式作業的名稱。

代理程式 作業名稱
提取訂閱的合併代理程式 <Publisher>-<PublicationDatabase>-<Publication>-<Subscriber>-<SubscriptionDatabase>-<integer>
發送訂閱的合併代理程式 <Publisher>-<PublicationDatabase>-<Publication>-<Subscriber>-<integer>
發送訂閱的散發代理程式 <Publisher>-<PublicationDatabase>-<Publication>-<Subscriber>-<integer>1
提取訂閱的散發代理程式 <Publisher>-<PublicationDatabase>-<Publication>-<Subscriber>-<SubscriptionDatabase>-<GUID>2
發送訂閱至非 SQL Server 訂閱者的散發代理程式 <Publisher>-<PublicationDatabase>-<Publication>-<Subscriber>-<integer>

1 針對 Oracle 發行集的發送訂閱,其作業名稱會是 <Publisher>-<Publisher>,而不是 <Publisher>-<PublicationDatabase>

2 針對 Oracle 發行集的提取訂閱,其作業名稱會是 <Publisher>-<DistributionDatabase>,而不是 <Publisher>-<PublicationDatabase>

若要指定同步排程

  1. 在 [新增訂閱精靈] 的 [同步排程] 頁面,從 [代理程式排程] 下拉式清單中為您正建立的每個訂閱選取下列值的其中之一:

    • 連續執行

    • [視需要執行]

    • <定義排程...>

  2. 如果您選取 [<定義排程...>],請在 [作業排程屬性] 對話方塊中指定排程,然後按一下 [確定]。

  3. 完成精靈。

若要在「複寫監視器」中修改發送訂閱的同步排程

  1. 在複寫監視器的左窗格中展開發行者群組,展開發行者,然後按一下發行集。

  2. 按一下 [所有訂閱] 索引標籤。

  3. 以滑鼠右鍵按一下訂閱,然後按一下 [檢視詳細資料]

  4. 在 [訂閱 <訂閱名稱>] 視窗中,按一下 [動作],然後按一下 [<代理程式名稱> 作業屬性]。

  5. 在 [作業屬性 - <作業名稱>] 對話方塊的 [排程] 頁面上,按一下 [編輯]。

  6. [作業排程屬性] 對話方塊中,從 [排程類型] 下拉式清單內選取一個值:

    • 若要指定代理程式應持續執行,請選取 [當 SQL Server Agent 啟動時自動啟動]

    • 若要指定代理程式應於排程上執行,請選取 [重複執行]

    • 若要指定代理程式應視需要執行,請選取 [執行一次]

  7. 若您選取 [重複執行] ,請為代理程式指定排程。

  8. 選取 [確定]。

若要在 Management Studio 中修改發送訂閱的同步排程

  1. 連線到 Management Studio 中的「散發者」,然後展開伺服器節點。

  2. 展開 [SQL Server Agent] 資料夾,然後展開 [作業] 資料夾。

  3. 以滑鼠右鍵按一下與訂閱相關聯的散發代理程式或合併代理程式的作業,然後按一下 [屬性]

  4. 在 [作業屬性 - <作業名稱>] 對話方塊的 [排程] 頁面上,按一下 [編輯]。

  5. [作業排程屬性] 對話方塊中,從 [排程類型] 下拉式清單內選取一個值:

    • 若要指定代理程式應持續執行,請選取 [當 SQL Server Agent 啟動時自動啟動]

    • 若要指定代理程式應於排程上執行,請選取 [重複執行]

    • 若要指定代理程式應視需要執行,請選取 [執行一次]

  6. 若您選取 [重複執行] ,請為代理程式指定排程。

  7. 選取 [確定]。

若要在 Management Studio 修改提取訂閱的同步排程

  1. 連線到 Management Studio 中的「訂閱者」,然後展開伺服器節點。

  2. 展開 [SQL Server Agent] 資料夾,然後展開 [作業] 資料夾。

  3. 以滑鼠右鍵按一下與訂閱相關聯的散發代理程式或合併代理程式的作業,然後按一下 [屬性]

  4. 在 [作業屬性 - <作業名稱>] 對話方塊的 [排程] 頁面上,按一下 [編輯]。

  5. [作業排程屬性] 對話方塊中,從 [排程類型] 下拉式清單內選取一個值:

    • 若要指定代理程式應持續執行,請選取 [當 SQL Server Agent 啟動時自動啟動]

    • 若要指定代理程式應於排程上執行,請選取 [重複執行]

    • 若要指定代理程式應視需要執行,請選取 [執行一次]

  6. 若您選取 [重複執行] ,請為代理程式指定排程。

  7. 選取 [確定]。

使用 TRANSACT-SQL

您可以使用複寫預存程序來以程式設計的方式定義同步排程。 使用哪些預存程序要依複寫的類型和訂閱的類型 (提取訂閱或發送訂閱) 而定。

排程是由下列排程參數所定義,其行為是繼承自 sp_add_schedule (Transact-SQL) (部分機器翻譯):

  • @frequency_type - 在排程代理程式時所使用的頻率類型。

  • @frequency_interval - 代理程式執行之一週中的日子。

  • @frequency_relative_interval - 當代理程式排定為要每月執行時,給定月份的週。

  • @frequency_recurrence_factor - 在同步處理之間發生的頻率類型單位數目。

  • @frequency_subday - 代理程式執行頻率超過一天一次以上時的頻率單位。

  • @frequency_subday_interval - 代理程式執行頻率超過一天一次以上時,執行之間的頻率單位數目。

  • @active_start_time_of_day - 代理程式在給定日子開始執行的最早時間。

  • @active_end_time_of_day - 代理程式在給定日子開始執行的最晚時間。

  • @active_start_date - 代理程式排程開始生效的第一天。

  • @active_end_date - 代理程式排程有效的最後一天。

針對交易式發行集的提取訂閱定義同步排程

  1. 建立交易式發行集的新提取訂閱。 如需詳細資訊,請參閱 建立提取訂閱

  2. 在訂閱者端,執行 sp_addpullsubscription_agent (Transact-SQL) (部分機器翻譯)。 指定 @publisher@publisher_db@publication 以及針對 @job_name@password 指定散發代理程式在訂閱者上執行時所使用的 Microsoft Windows 認證。 指定以上詳述的同步處理參數,這些參數會針對同步處理訂閱的散發代理程式作業定義排程。

針對交易式發行集的發送訂閱定義同步排程

  1. 建立交易式發行集的新發送訂閱。 如需詳細資訊,請參閱 建立發送訂閱

  2. 在訂閱者端,執行 sp_addpushsubscription_agent (Transact-SQL) (部分機器翻譯)。 指定 @subscriber@subscriber_db@publication以及針對 @job_name@password指定散發代理程式在訂閱者上執行時所使用的 Windows 認證。 指定以上詳述的同步處理參數,這些參數會針對同步處理訂閱的散發代理程式作業定義排程。

針對合併式發行集的提取訂閱定義同步排程

  1. 建立合併式發行集的新提取訂閱。 如需詳細資訊,請參閱 建立提取訂閱

  2. 在訂閱者上,執行 sp_addmergepullsubscription_agent。 指定 @publisher@publisher_db@publication以及針對 @job_name@password指定合併代理程式在訂閱者上執行時所使用的 Windows 認證。 指定以上詳述的同步處理參數,這些參數會針對同步處理訂閱的合併代理程式作業定義排程。

針對合併式發行集的發送訂閱定義同步排程

  1. 建立合併式發行集的新發送訂閱。 如需詳細資訊,請參閱 建立發送訂閱

  2. 在訂閱者上,執行 sp_addmergepushsubscription_agent。 指定 @subscriber@subscriber_db@publication以及針對 @job_name@password指定合併代理程式在訂閱者上執行時所使用的 Windows 認證。 指定以上詳述的同步處理參數,這些參數會針對同步處理訂閱的合併代理程式作業定義排程。

使用 Replication Management Objects (RMO)

複寫會使用 SQL Server Agent 來排程定期發生之活動的作業,例如快照集的產生和訂閱同步處理。 您可以使用Replication Management Objects (RMO),以程式設計方式指定複寫代理程式作業的排程。

注意

當您建立訂閱,並針對 false (提取訂閱的預設行為) 指定 CreateSyncAgentByDefault 的值時,不會建立代理程式作業,而且會忽略排程屬性。 在此情況下,同步排程必須由應用程式來決定。 如需相關資訊,請參閱 Create a Pull SubscriptionCreate a Push Subscription

當您建立交易式發行集的發送訂閱時,定義複寫代理程式排程

  1. 為您所建立的訂閱建立 TransSubscription 類別的執行個體。 如需詳細資訊,請參閱 建立發送訂閱

  2. 在您呼叫 Create之前,請設定 AgentSchedule 屬性的下列一或多個欄位:

    注意

    若未指定這些屬性的其中一個,則會設定預設值。

  3. 呼叫 Create 方法來建立訂閱。

當您建立交易式發行集的提取訂閱時,定義複寫代理程式排程

  1. 為您所建立的訂閱建立 TransPullSubscription 類別的執行個體。 如需詳細資訊,請參閱 建立提取訂閱

  2. 在您呼叫 Create之前,請設定 AgentSchedule 屬性的下列一或多個欄位:

    注意

    若未指定這些屬性的其中一個,則會設定預設值。

  3. 呼叫 Create 方法來建立訂閱。

當您建立合併式發行集的提取訂閱時,定義複寫代理程式排程

  1. 為您所建立的訂閱建立 MergePullSubscription 類別的執行個體。 如需詳細資訊,請參閱 建立提取訂閱

  2. 在您呼叫 Create之前,請設定 AgentSchedule 屬性的下列一或多個欄位:

    注意

    若未指定這些屬性的其中一個,則會設定預設值。

  3. 呼叫 Create 方法來建立訂閱。

當您建立合併式發行集的發送訂閱時,定義複寫代理程式排程

  1. 為您所建立的訂閱建立 MergeSubscription 類別的執行個體。 如需詳細資訊,請參閱 建立發送訂閱

  2. 在您呼叫 Create之前,請設定 AgentSchedule 屬性的下列一或多個欄位:

    注意

    若未指定這些屬性的其中一個,則會設定預設值。

  3. 呼叫 Create 方法來建立訂閱。

範例 (RMO)

此範例會建立合併式發行集的發送訂閱,並指定同步處理此訂閱所依據的排程。

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";
string hostname = @"adventure-works\garrett1";

//Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

// Create the objects that we need.
MergePublication publication;
MergeSubscription subscription;

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Ensure that the publication exists and that 
    // it supports push subscriptions.
    publication = new MergePublication();
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;
    publication.ConnectionContext = conn;

    if (publication.IsExistingObject)
    {
        if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
        {
            publication.Attributes |= PublicationAttributes.AllowPush;
        }

        // Define the push subscription.
        subscription = new MergeSubscription();
        subscription.ConnectionContext = conn;
        subscription.SubscriberName = subscriberName;
        subscription.PublicationName = publicationName;
        subscription.DatabaseName = publicationDbName;
        subscription.SubscriptionDBName = subscriptionDbName;
        subscription.HostName = hostname;

        // Set a schedule to synchronize the subscription every 2 hours
        // during weekdays from 6am to 10pm.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly;
        subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32(0x003E);
        subscription.AgentSchedule.FrequencyRecurrenceFactor = 1;
        subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour;
        subscription.AgentSchedule.FrequencySubDayInterval = 2;
        subscription.AgentSchedule.ActiveStartDate = 20051108;
        subscription.AgentSchedule.ActiveEndDate = 20071231;
        subscription.AgentSchedule.ActiveStartTime = 060000;
        subscription.AgentSchedule.ActiveEndTime = 100000;

        // Specify the Windows login credentials for the Merge Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword;

        // Create the push subscription.
        subscription.Create();
    }
    else
    {
        // Do something here if the publication does not exist.
        throw new ApplicationException(String.Format(
            "The publication '{0}' does not exist on {1}.",
            publicationName, publisherName));
    }
}
catch (Exception ex)
{
    // Implement the appropriate error handling here.
    throw new ApplicationException(String.Format(
        "The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"
Dim hostname As String = "adventure-works\garrett1"

'Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergeSubscription

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Ensure that the publication exists and that 
    ' it supports push subscriptions.
    publication = New MergePublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = conn

    If publication.IsExistingObject Then
        If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPush
        End If

        ' Define the push subscription.
        subscription = New MergeSubscription()
        subscription.ConnectionContext = conn
        subscription.SubscriberName = subscriberName
        subscription.PublicationName = publicationName
        subscription.DatabaseName = publicationDbName
        subscription.SubscriptionDBName = subscriptionDbName
        subscription.HostName = hostname

        ' Set a schedule to synchronize the subscription every 2 hours
        ' during weekdays from 6am to 10pm.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly
        subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32("0x003E", 16)
        subscription.AgentSchedule.FrequencyRecurrenceFactor = 1
        subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour
        subscription.AgentSchedule.FrequencySubDayInterval = 2
        subscription.AgentSchedule.ActiveStartDate = 20051108
        subscription.AgentSchedule.ActiveEndDate = 20071231
        subscription.AgentSchedule.ActiveStartTime = 60000
        subscription.AgentSchedule.ActiveEndTime = 100000

        ' Specify the Windows login credentials for the Merge Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword

        ' Create the push subscription.
        subscription.Create()
    Else

        ' Do something here if the publication does not exist.
        Throw New ApplicationException(String.Format( _
         "The publication '{0}' does not exist on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
    "The subscription to {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try

另請參閱

複寫安全性最佳作法
訂閱發行集
同步處理發送訂閱
同步處理提取訂閱
同步處理資料