{"id":1566,"date":"2016-07-28T07:47:40","date_gmt":"2016-07-28T01:47:40","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1566"},"modified":"2016-07-28T07:47:40","modified_gmt":"2016-07-28T01:47:40","slug":"sql-server-replication-status","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1566","title":{"rendered":"SQL Server Replication status"},"content":{"rendered":"<p>Few days back I had to query to my production sql replication server to find what all publications are running and what is the status for each of those. A search on the web found me this (unfortunately I don&#8217;t know the author of this to give credit ) :<br \/>\nSELECT<br \/>\n(CASE<br \/>\nWHEN mdh.runstatus = &#8216;1&#8217; THEN &#8216;Start &#8211; &#8216;+cast(mdh.runstatus as varchar)<br \/>\nWHEN mdh.runstatus = &#8216;2&#8217; THEN &#8216;Succeed &#8211; &#8216;+cast(mdh.runstatus as varchar)<br \/>\nWHEN mdh.runstatus = &#8216;3&#8217; THEN &#8216;InProgress &#8211; &#8216;+cast(mdh.runstatus as varchar)<br \/>\nWHEN mdh.runstatus = &#8216;4&#8217; THEN &#8216;Idle &#8211; &#8216;+cast(mdh.runstatus as varchar)<br \/>\nWHEN mdh.runstatus = &#8216;5&#8217; THEN &#8216;Retry &#8211; &#8216;+cast(mdh.runstatus as varchar)<br \/>\nWHEN mdh.runstatus = &#8216;6&#8217; THEN &#8216;Fail &#8211; &#8216;+cast(mdh.runstatus as varchar)<br \/>\nELSE CAST(mdh.runstatus AS VARCHAR)<br \/>\nEND) [Run Status],<br \/>\nmda.subscriber_db [Subscriber DB],<br \/>\nmda.publication [PUB Name],<br \/>\nright(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=&#8217;ALL&#8217; then 1 else LEN(mda.publication)+2 end))) [SUBSCRIBER],<br \/>\nCONVERT(VARCHAR(25),mdh.[time]) [LastSynchronized],<br \/>\nund.UndelivCmdsInDistDB [UndistCom],<br \/>\nmdh.comments [Comments],<br \/>\n&#8216;select * from distribution.dbo.msrepl_errors (nolock) where id = &#8216; + CAST(mdh.error_id AS VARCHAR(8)) [Query More Info],<br \/>\nmdh.xact_seqno [SEQ_NO],<br \/>\n(CASE<br \/>\nWHEN mda.subscription_type = &#8216;0&#8217; THEN &#8216;Push&#8217;<br \/>\nWHEN mda.subscription_type = &#8216;1&#8217; THEN &#8216;Pull&#8217;<br \/>\nWHEN mda.subscription_type = &#8216;2&#8217; THEN &#8216;Anonymous&#8217;<br \/>\nELSE CAST(mda.subscription_type AS VARCHAR)<br \/>\nEND) [SUB Type],<\/p>\n<p>mda.publisher_db+&#8217; &#8211; &#8216;+CAST(mda.publisher_database_id as varchar) [Publisher DB],<br \/>\nmda.name [Pub &#8211; DB &#8211; Publication &#8211; SUB &#8211; AgentID]<br \/>\nFROM distribution.dbo.MSdistribution_agents mda<br \/>\nLEFT JOIN distribution.dbo.MSdistribution_history mdh ON mdh.agent_id = mda.id<br \/>\nJOIN<br \/>\n(SELECT s.agent_id, MaxAgentValue.[time], SUM(CASE WHEN xact_seqno &gt; MaxAgentValue.maxseq THEN 1 ELSE 0 END) AS UndelivCmdsInDistDB<br \/>\nFROM distribution.dbo.MSrepl_commands t (NOLOCK)<br \/>\nJOIN distribution.dbo.MSsubscriptions AS s (NOLOCK) ON (t.article_id = s.article_id AND t.publisher_database_id=s.publisher_database_id )<br \/>\nJOIN<br \/>\n(SELECT hist.agent_id, MAX(hist.[time]) AS [time], h.maxseq<br \/>\nFROM distribution.dbo.MSdistribution_history hist (NOLOCK)<br \/>\nJOIN (SELECT agent_id,ISNULL(MAX(xact_seqno),0x0) AS maxseq<br \/>\nFROM distribution.dbo.MSdistribution_history (NOLOCK)<br \/>\nGROUP BY agent_id) AS h<br \/>\nON (hist.agent_id=h.agent_id AND h.maxseq=hist.xact_seqno)<br \/>\nGROUP BY hist.agent_id, h.maxseq<br \/>\n) AS MaxAgentValue<br \/>\nON MaxAgentValue.agent_id = s.agent_id<br \/>\nGROUP BY s.agent_id, MaxAgentValue.[time]<br \/>\n) und<br \/>\nON mda.id = und.agent_id AND und.[time] = mdh.[time]<br \/>\nwhere mda.subscriber_db&lt;&gt;&#8217;virtual&#8217; &#8212; 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.<br \/>\n&#8211;and mdh.runstatus=&#8217;6&#8242; &#8211;Fail<br \/>\n&#8211;and mdh.runstatus&lt;&gt;&#8217;2&#8242; &#8211;Succeed<br \/>\norder by mdh.[time]<\/p>\n<p>The script can be downloaded \u00c2\u00a0<a href=\"https:\/\/techsatwork.com\/blog\/wp-content\/uploads\/2016\/07\/Replication-verification.txt\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Few days back I had to query to my production sql replication server to find what all publications are running and what is the status for each of those. A search on the web found me this (unfortunately I don&#8217;t know the author of this to give credit ) : SELECT (CASE WHEN mdh.runstatus = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[303,358,658],"tags":[932,930,929,928,931],"class_list":["post-1566","post","type-post","status-publish","format-standard","hentry","category-database","category-how-to","category-sql-server-2","tag-distribution","tag-publication","tag-replication","tag-sql-replication","tag-subscriber"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1566"}],"version-history":[{"count":1,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1566\/revisions"}],"predecessor-version":[{"id":1568,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1566\/revisions\/1568"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}