如要將 SQL Server 例項設定為搭配 Always On 可用性群組使用,您必須啟用 SQL Server Agent,以便在發生容錯移轉時擷取記錄,然後執行清理工作。您必須先修改 CDC 代理程式工作步驟,才能確認目前的複本是否確實為主要複本。這項操作是使用 sys.fn_hadr_is_primary_replica 函式完成。
請使用下列指令設定執行個體:
-- Check if the current replica is a primary for the corresponding database.USE[DATABASE_NAME];DECLARE@DatabaseNameSYSNAME=DB_NAME();IF(SELECTsys.fn_hadr_is_primary_replica(@DatabaseName))=1BEGIN-- If the replica isn't a primary, the code block that follows is skippedEXECUTEsys.sp_cdc_add_job@job_type='capture';EXECUTEsys.sp_cdc_add_job@job_type='cleanup';END
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Work with Always On availability groups\n\nThis page describes what Always-On availability groups in SQL Server are and how\nDatastream supports them for failover and data recovery scenarios.\n\nOverview of Always On availability groups\n-----------------------------------------\n\nIn SQL Server, Always On availability groups are a high-availability solution\nthat lets you prepare your databases for disaster recovery scenarios.\n\nAlways On availability groups maximize database availability for businesses.\nAvailability groups support a replicated environment for a selected set of\ndatabases, known as availability databases. Each group includes one set of\nprimary databases for read and write tasks, and up to eight sets of corresponding\nsecondary databases. The secondary databases can optionally allow read-only\naccess or backup operations.\n\nFor more information about Always On availability groups, see\n[What is an Always On availability group?](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server) in the SQL Server\ndocumentation.\n\nFor information about Always On Availability group prerequisites for a SQL\nServer instance, see the [SQL Server documentation](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/prereqs-restrictions-recommendations-always-on-availability?view=sql-server-ver17&source=recommendations#ServerInstance).\n\nConfigure Datastream for use with Always On availability groups\n---------------------------------------------------------------\n\nDatastream supports *synchronous-commit* availability mode with the\nchange tables CDC method. Under this mode, the secondary database stays\nsynchronised with the corresponding primary until data synchronization stops.\nThe confirmation of a transaction is only sent to the client when the secondary\nreplica writes the incoming transaction log records to a disk.\n\nFor information about availability modes, see\n[Differences between availability modes for an Always On availability group](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server).\n\nTo configure your SQL Server instance for use with Always On availability groups,\nyou need to enable SQL Server Agent to capture logs when there's a failover, and\nthen run a clean up job. Before you can do this, you need to modify the CDC\nAgent job steps to check if the current replica is actually the primary. This is\ndone using the `sys.fn_hadr_is_primary_replica` function.\n\nUse the following commands to set up your instance: \n\n -- Check if the current replica is a primary for the corresponding database.\n USE [\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDATABASE_NAME\u003c/span\u003e\u003c/var\u003e];\n DECLARE @DatabaseName SYSNAME = DB_NAME();\n IF (SELECT sys.fn_hadr_is_primary_replica(@DatabaseName)) = 1\n BEGIN\n -- If the replica isn't a primary, the code block that follows is skipped\n EXECUTE sys.sp_cdc_add_job @job_type = 'capture';\n EXECUTE sys.sp_cdc_add_job @job_type = 'cleanup';\n END\n\nWhat's next\n-----------\n\n- Learn more about how Datastream works with [SQL Server sources](/datastream/docs/sources-sqlserver)."]]