20

我希望能够在 SQL Server 2008 T-SQL 中检查发布和订阅的状态。我希望能够确定它是否可以,最后一次成功是什么时候,同步等等。这可能吗?

4

3 回答 3

38

我知道这有点晚了......

SELECT 
(CASE  
    WHEN mdh.runstatus =  '1' THEN 'Start - '+cast(mdh.runstatus as varchar)
    WHEN mdh.runstatus =  '2' THEN 'Succeed - '+cast(mdh.runstatus as varchar)
    WHEN mdh.runstatus =  '3' THEN 'InProgress - '+cast(mdh.runstatus as varchar)
    WHEN mdh.runstatus =  '4' THEN 'Idle - '+cast(mdh.runstatus as varchar)
    WHEN mdh.runstatus =  '5' THEN 'Retry - '+cast(mdh.runstatus as varchar)
    WHEN mdh.runstatus =  '6' THEN 'Fail - '+cast(mdh.runstatus as varchar)
    ELSE CAST(mdh.runstatus AS VARCHAR)
END) [Run Status], 
mda.subscriber_db [Subscriber DB], 
mda.publication [PUB Name],
right(left(mda.name,LEN(mda.name)-(len(mda.id)+1)), LEN(left(mda.name,LEN(mda.name)-(len(mda.id)+1)))-(10+len(mda.publisher_db)+(case when mda.publisher_db='ALL' then 1 else LEN(mda.publication)+2 end))) [SUBSCRIBER],
CONVERT(VARCHAR(25),mdh.[time]) [LastSynchronized],
und.UndelivCmdsInDistDB [UndistCom], 
mdh.comments [Comments], 
'select * from distribution.dbo.msrepl_errors (nolock) where id = ' + CAST(mdh.error_id AS VARCHAR(8)) [Query More Info],
mdh.xact_seqno [SEQ_NO],
(CASE  
    WHEN mda.subscription_type =  '0' THEN 'Push' 
    WHEN mda.subscription_type =  '1' THEN 'Pull' 
    WHEN mda.subscription_type =  '2' THEN 'Anonymous' 
    ELSE CAST(mda.subscription_type AS VARCHAR)
END) [SUB Type],

mda.publisher_db+' - '+CAST(mda.publisher_database_id as varchar) [Publisher DB],
mda.name [Pub - DB - Publication - SUB - AgentID]
FROM distribution.dbo.MSdistribution_agents mda 
LEFT JOIN distribution.dbo.MSdistribution_history mdh ON mdh.agent_id = mda.id 
JOIN 
    (SELECT s.agent_id, MaxAgentValue.[time], SUM(CASE WHEN xact_seqno > MaxAgentValue.maxseq THEN 1 ELSE 0 END) AS UndelivCmdsInDistDB 
    FROM distribution.dbo.MSrepl_commands t (NOLOCK)  
    JOIN distribution.dbo.MSsubscriptions AS s (NOLOCK) ON (t.article_id = s.article_id AND t.publisher_database_id=s.publisher_database_id ) 
    JOIN 
        (SELECT hist.agent_id, MAX(hist.[time]) AS [time], h.maxseq  
        FROM distribution.dbo.MSdistribution_history hist (NOLOCK) 
        JOIN (SELECT agent_id,ISNULL(MAX(xact_seqno),0x0) AS maxseq 
        FROM distribution.dbo.MSdistribution_history (NOLOCK)  
        GROUP BY agent_id) AS h  
        ON (hist.agent_id=h.agent_id AND h.maxseq=hist.xact_seqno) 
        GROUP BY hist.agent_id, h.maxseq 
        ) AS MaxAgentValue 
    ON MaxAgentValue.agent_id = s.agent_id 
    GROUP BY s.agent_id, MaxAgentValue.[time] 
    ) und 
ON mda.id = und.agent_id AND und.[time] = mdh.[time] 
where mda.subscriber_db<>'virtual' -- created when your publication has the immediate_sync property set to true. This property dictates whether snapshot is available all the time for new subscriptions to be initialized. This affects the cleanup behavior of transactional replication. If this property is set to true, the transactions will be retained for max retention period instead of it getting cleaned up as soon as all the subscriptions got the change.
--and mdh.runstatus='6' --Fail
--and mdh.runstatus<>'2' --Succeed
order by mdh.[time]
于 2010-10-27T18:29:01.303 回答
9

旧帖子但为其他人添加 - 以下存储过程将为您提供所需的内容:

它允许您查看分发者的所有复制发布的状态和其他信息(在分发数据库上运行它)

于 2015-02-11T10:49:29.793 回答
0

如果有帮助,我将给出的两个答案的一部分结合起来,并采用 sp_replmonitorhelppublication 和 sp_replmonitorhelpsubscription 并将它们放入临时表中,以便我可以对它们进行排序并根据需要排除列。

请注意,订阅者脚本从 sp_replmonitorhelpsubscription 中排除了一些合并列,因为我不是在寻找合并复制数据。

-------------------------------------------------------------------------------------------------------------------------
--  PUBLISHER SCRIPT
-------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID ('tempdb..#tmp_replicationPub_monitordata') IS NOT NULL DROP TABLE #tmp_replicationPub_monitordata;

CREATE TABLE #tmp_replicationPub_monitordata (
    publisher_db             sysname
  , publication              sysname
  , publication_id           INT
  , publication_type         INT
  , status                   INT        -- publication status defined as max(status) among all agents
  , warning                  INT        -- publication warning defined as max(isnull(warning,0)) among all agents
  , worst_latency            INT
  , best_latency             INT
  , average_latency          INT
  , last_distsync            DATETIME   -- last sync time
  , retention                INT        -- retention period                                               
  , latencythreshold         INT
  , expirationthreshold      INT
  , agentnotrunningthreshold INT
  , subscriptioncount        INT        -- # of subscription
  , runningdistagentcount    INT        -- # of running agents
  , snapshot_agentname       sysname NULL
  , logreader_agentname      sysname NULL
  , qreader_agentname        sysname NULL
  , worst_runspeedPerf       INT
  , best_runspeedPerf        INT
  , average_runspeedPerf     INT
  , retention_period_unit    TINYINT
  , publisher                sysname NULL
);

INSERT INTO #tmp_replicationPub_monitordata
EXEC sp_replmonitorhelppublication;

SELECT      (CASE WHEN status = '1' THEN 'Start - ' + CAST(status AS VARCHAR)
                 WHEN status = '2' THEN 'Succeed - ' + CAST(status AS VARCHAR)
                 WHEN status = '3' THEN 'InProgress - ' + CAST(status AS VARCHAR)
                 WHEN status = '4' THEN 'Idle - ' + CAST(status AS VARCHAR)
                 WHEN status = '5' THEN 'Retry - ' + CAST(status AS VARCHAR)
                 WHEN status = '6' THEN 'Fail - ' + CAST(status AS VARCHAR)ELSE CAST(status AS VARCHAR)END)  [Run Status]
          , publisher_db
          , publication
          , publication_id
          , (CASE WHEN publication_type = '0' THEN 'Transactional - ' + CAST(publication_type AS VARCHAR)
                 WHEN publication_type = '1' THEN 'Snapshot - ' + CAST(publication_type AS VARCHAR)
                 WHEN publication_type = '2' THEN 'Merge - ' + CAST(publication_type AS VARCHAR)ELSE '' END) AS [Publication Type]
          , (CASE WHEN warning = '1' THEN 'Expiration' + CAST(warning AS VARCHAR)
                 WHEN warning = '2' THEN 'Latency' + CAST(warning AS VARCHAR)
                 WHEN warning = '4' THEN 'Mergeexpiration' + CAST(warning AS VARCHAR)
                 WHEN warning = '16' THEN 'Mergeslowrunduration' + CAST(warning AS VARCHAR)
                 WHEN warning = '32' THEN 'Mergefastrunspeed' + CAST(warning AS VARCHAR)
                 WHEN warning = '64' THEN 'Mergeslowrunspeed' + CAST(warning AS VARCHAR)END)                 warning
          , worst_latency
          , best_latency
          , average_latency
          , last_distsync
          , retention
          , latencythreshold
          , expirationthreshold
          , agentnotrunningthreshold
          , subscriptioncount
          , runningdistagentcount
          , snapshot_agentname
          , logreader_agentname
          , qreader_agentname
          , worst_runspeedPerf
          , best_runspeedPerf
          , average_runspeedPerf
          , retention_period_unit
          , publisher
FROM        #tmp_replicationPub_monitordata
ORDER BY    publication;

-------------------------------------------------------------------------------------------------------------------------
--  SUBSCRIBER SCRIPT
-------------------------------------------------------------------------------------------------------------------------
IF OBJECT_ID ('tempdb..#tmp_rep_monitordata  ') IS NOT NULL DROP TABLE #tmp_rep_monitordata;

CREATE TABLE #tmp_rep_monitordata (
    status                        INT        NULL
  , warning                       INT        NULL
  , subscriber                    sysname    NULL
  , subscriber_db                 sysname    NULL
  , publisher_db                  sysname    NULL
  , publication                   sysname    NULL
  , publication_type              INT        NULL
  , subtype                       INT        NULL
  , latency                       INT        NULL
  , latencythreshold              INT        NULL
  , agentnotrunning               INT        NULL
  , agentnotrunningthreshold      INT        NULL
  , timetoexpiration              INT        NULL
  , expirationthreshold           INT        NULL
  , last_distsync                 DATETIME   NULL
  , distribution_agentname        sysname    NULL
  , mergeagentname                sysname    NULL
  , mergesubscriptionfriendlyname sysname    NULL
  , mergeagentlocation            sysname    NULL
  , mergeconnectiontype           INT        NULL
  , mergePerformance              INT        NULL
  , mergerunspeed                 FLOAT      NULL
  , mergerunduration              INT        NULL
  , monitorranking                INT        NULL
  , distributionagentjobid        BINARY(16) NULL
  , mergeagentjobid               BINARY(16) NULL
  , distributionagentid           INT        NULL
  , distributionagentprofileid    INT        NULL
  , mergeagentid                  INT        NULL
  , mergeagentprofileid           INT        NULL
  , logreaderagentname            sysname    NULL
  , publisher                     sysname    NULL
);

INSERT INTO #tmp_rep_monitordata
EXEC sp_replmonitorhelpsubscription @publication_type = 0;

INSERT INTO #tmp_rep_monitordata
EXEC sp_replmonitorhelpsubscription @publication_type = 1;

SELECT      (CASE WHEN status = '1' THEN 'Start - ' + CAST(status AS VARCHAR)
                 WHEN status = '2' THEN 'Succeed - ' + CAST(status AS VARCHAR)
                 WHEN status = '3' THEN 'InProgress - ' + CAST(status AS VARCHAR)
                 WHEN status = '4' THEN 'Idle - ' + CAST(status AS VARCHAR)
                 WHEN status = '5' THEN 'Retry - ' + CAST(status AS VARCHAR)
                 WHEN status = '6' THEN 'Fail - ' + CAST(status AS VARCHAR)ELSE CAST(status AS VARCHAR)END)  [Run Status]
          , publisher_db
          , publication
          , (CASE WHEN warning = '1' THEN 'Expiration' + CAST(warning AS VARCHAR)
                 WHEN warning = '2' THEN 'Latency' + CAST(warning AS VARCHAR)
                 WHEN warning = '4' THEN 'Mergeexpiration' + CAST(warning AS VARCHAR)
                 WHEN warning = '16' THEN 'Mergeslowrunduration' + CAST(warning AS VARCHAR)
                 WHEN warning = '32' THEN 'Mergefastrunspeed' + CAST(warning AS VARCHAR)
                 WHEN warning = '64' THEN 'Mergeslowrunspeed' + CAST(warning AS VARCHAR)END)                 warning
          , subscriber
          , subscriber_db
          , (CASE WHEN publication_type = '0' THEN 'Transactional - ' + CAST(publication_type AS VARCHAR)
                 WHEN publication_type = '1' THEN 'Snapshot - ' + CAST(publication_type AS VARCHAR)
                 WHEN publication_type = '2' THEN 'Merge - ' + CAST(publication_type AS VARCHAR)ELSE '' END) AS [Publication Type]
          , (CASE WHEN subtype = '0' THEN 'Push - ' + CAST(subtype AS VARCHAR)
                 WHEN subtype = '1' THEN 'Pull - ' + CAST(subtype AS VARCHAR)
                 WHEN subtype = '2' THEN 'Anonymous - ' + CAST(subtype AS VARCHAR)ELSE '' END)               AS SubscriptionType
          , latency
          , latencythreshold
          , agentnotrunning
          , agentnotrunningthreshold
          , last_distsync
          , timetoexpiration
          , expirationthreshold
          , distribution_agentname
          , monitorranking
          , distributionagentjobid
          , mergeagentjobid
          , distributionagentid
          , distributionagentprofileid
          , mergeagentid
          , mergeagentprofileid
          , logreaderagentname
          , publisher
FROM        #tmp_rep_monitordata
ORDER BY    publication ASC;
于 2022-01-05T13:45:27.237 回答