sp_replmonitorsubscriptionpendingcmds到表[How2]

时间:2014-02-09 14:04:48

标签: sql-server tsql sql-server-2005

我正在寻找一份工作,告诉我复制中挂起的cmd的数量, 我正在使用sp_replmonitorsubscriptionpendingcmds并尽我所能将其放入表中。

但我一直在收到错误 An INSERT EXEC statement cannot be nested. 任何想法怎么做? 我愿意接受建议

谢谢

我试过了:

DECLARE @tab AS TABLE (pendingcmds int, estimatedprocesstime int) 

INSERT into @tab EXECUTE  sp_executesql N'exec sp_replmonitorsubscriptionpendingcmds   @publisher=[AUTOSQL1\COMPLOT],  
@publisher_db=PTK_M,  @publication=PTK_M_1way,  
@subscriber=[COMPLOTSQL2008\REPL],  @subscriber_db=
PTK_M,  @subscription_type=0'
  SELECT * FROM @tab

insert pendingcmds   (pendingcmdcount ,estimatedprocesstime) 
execute sp_replmonitorsubscriptionpendingcmds   
@publisher=[AUTOSQL1\COMPLOT],  @publisher_db=PTK_M,  
@publication=PTK_M_1way,  @subscriber=[COMPLOTSQL2008\REPL],  
@subscriber_db=PTK_M,  @subscription_type=0
into pendingcmds   (pendingcmdcount ,estimatedprocesstime)

declare @v nvarchar (max)
set @v=N'exec sp_replmonitorsubscriptionpendingcmds   @publisher=[AUTOSQL1\COMPLOT],  
@publisher_db=PTK_M,  @publication=PTK_M_1way,  
@subscriber=[COMPLOTSQL2008\REPL],  @subscriber_db=
PTK_M,  @subscription_type=0'
print @v
INSERT INTO pendingcmds (pendingcmdcount,estimatedprocesstime)
exec sp_executesql @v

3 个答案:

答案 0 :(得分:1)

sp_replmonitorsubscriptionpendingcmds在内部调用以下内容,SQL Server将其视为嵌套插入。

insert into @countab (pendingcmdcount)
exec @retcode = sys.sp_MSget_repl_commands 
                @agent_id = @agent_id,
                @last_xact_seqno = @xact_seqno,
                @get_count = 2,
                @compatibility_level = 9000000

这是一个关于变通方法的好博客:SimpleTalk Monitoring Transactional Replication in SQL Server by Francis Hanlon

答案 1 :(得分:0)

我想我有你需要的东西。

这是一个有点长的脚本,但如果您使用复制(事务),它会很有用。

它假定您的分发数据库名为 distribution - 我的服务器有多个分发数据库,​​因此您需要指定其名称。

此脚本将在分发服务器或发布者(如果它也配置为分发服务器)上运行,基本上位于您 distribution 数据库所在的任何位置。

检查一下:

        --====================================================================================
        -- I got this script from Mohammed Mawla - all glories to him!
        -- I did some changes
        -- it works in most of my servers, as long permissions are granted on both the publisher and distributor
        -- I get sometimes errors like these:

        -- publisher server name is encrypted or distributor server does not have right to access the publisher
        -- Msg 20032, Level 16, State 1, Procedure sp_replmonitorsubscriptionpendingcmds, Line 52 [Batch Start Line 23]
        -- '[28D09263-3FCA-42F2-86FD-1C9C7CA3951A]' is not defined as a Subscriber for 'SQLPROD1'.

        -- Marcello Miorelli
        -- 30-dec-2019
        --====================================================================================

Use Master;
GO
Set NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
GO

IF object_id('tempdb..#Radhe_sub2') is not null
   DROP TABLE #Radhe_sub2

IF object_id('tempdb..#Radhe_sub1') is not null
   DROP TABLE #Radhe_sub1

Create TABLE #Radhe_sub2 ( publisher SYSNAME 
                        , publisher_db SYSNAME
                        , publication SYSNAME
                        , subscriber SYSNAME
                        , subscriber_db SYSNAME
                        , Pending_Commands int 
                        , time_to_deliver_pending_Commands int)

DECLARE 
  @publisher SYSNAME
, @publisher_db SYSNAME
, @publication SYSNAME
, @subscriber SYSNAME
, @subscriber_db SYSNAME

SELECT
sub3.publisher
,sub1.publisher_db
,sub1.publication
,CASE when sub1.anonymous_subid is not null then upper(sub1.subscriber_name) ELSE UPPER (srv.name) END 'Subscriber'
,sub1.subscriber_db
,Sub1.job_id,sub1.id
,subscription_type
,sub1.name
into #Radhe_sub1
FROM
(
SELECT * 
  FROM distribution..msdistribution_agents agents
 Where subscriber_db not in ('virtual') -- Don't retrieve Virtual subscriptions
   and anonymous_subid is null -- Don't retrieve anonymous subscriptions

) sub1
Inner join
(
SELECT
publisher
,publisher_db
,publication
,publication_type
,agent_name
,publisher_srvid
,job_id
FROM distribution..MSreplication_monitordata
WHERE publication_id is not null
AND agent_type = 3 -- Distribution agent
)sub3

on sub1.publisher_id = sub3.publisher_srvid
and cast(sub1.job_id as uniqueidentifier) = sub3.job_id
and sub1.publisher_db=sub3.publisher_db
and sub1.publication= sub3.publication
and sub1.subscription_type=sub3.publication_type
and sub1.name =sub3.agent_name
join master.sys.servers as srv
on srv.server_id = sub1.subscriber_id

DECLARE subscribers cursor READ_ONLY FAST_FORWARD for 
SELECT 
  publisher
, publisher_db 
, publication 
, subscriber 
, subscriber_db 
from #Radhe_sub1

OPEN subscribers FETCH NEXT FROM subscribers INTO @publisher
, @publisher_db 
, @publication 
, @subscriber 
, @subscriber_db

WHILE @@FETCH_STATUS = 0 BEGIN

--select @subscriber = N'CTDB01'

--begin try
        INSERT into #Radhe_sub2
        EXEC
        (
        '
        SELECT '''+ @publisher +''' 
                , '''+ @publisher_db +''' 
                ,'''+ @publication + ''' 
                , ''' + @subscriber + ''' 
                , ''' + @subscriber_db + ''' 
                ,*
                        FROM OPENROWSET (''SQLOLEDB''
                ,''Server='+@@servername+';TRUSTED_CONNECTION=YES;''
                ,''set fmtonly off EXEC distribution..sp_replmonitorsubscriptionpENDingcmds @publisher= '''''+ @publisher +''''' 
                ,@subscription_type=0
                , @publisher_db= '''''+ @publisher_db +'''''
                ,@publication = '''''+ @publication+'''''
                ,@subscriber= '''''+@subscriber+''''' 
                ,@subscriber_db='''''+@subscriber_db+''''''')
        '

        --====================================================================================
        -- the problem with this line below is that it does not work with named instances Server=(local)
        --'
        --SELECT '''+ @publisher +''' 
        --, '''+ @publisher_db +''' 
        --,'''+ @publication + ''' 
        --, ''' + @subscriber + ''' 
        --, ''' + @subscriber_db + ''' 
        --,*
        --  FROM OPENROWSET (''SQLOLEDB''
        --,''Server=(local);TRUSTED_CONNECTION=YES;''
        --,''set fmtonly off EXEC distribution..sp_replmonitorsubscriptionpENDingcmds @publisher= '''''+ @publisher +''''' 
        --,@subscription_type=0
        --, @publisher_db= '''''+ @publisher_db +'''''
        --,@publication = '''''+ @publication+'''''
        --,@subscriber= '''''+@subscriber+''''' 
        --,@subscriber_db='''''+@subscriber_db+''''''')
        --'
        --====================================================================================

        )
--end try
--begin catch
--end catch


FETCH NEXT 
FROM subscribers 
INTO 
 @publisher
,@publisher_db 
,@publication 
,@subscriber 
,@subscriber_db

END

CLOSE subscribers DEALLOCATE subscribers

SELECT
Pending_commands.*
,comment.comments
,comment.delivery_latency 'Delivery_latency MSs'
,comment.time 'Time of message'
,CASE comment.runstatus
when 1 then 'Started'
when 2 then 'Succeeded'
when 3 then 'In progress'
when 4 then 'Idle'
when 5 then 'Retrying'
when 6 then 'Failed ' END status ,

CASE Info.subscription_type When 0 then 'Push' When 1 then 'Pull' When 2 then 'Anonymous' END 'Subscription Type'
,Info.name 'Distribution agent name'
,jobs.name 'Distribution_agent_job'

FROM

#Radhe_sub1 Info

inner join

#Radhe_sub2 PENDing_commands

on Info.publisher_db = PENDing_commands.publisher_db
and Info.publication = PENDing_commands.publication
and Info.subscriber = PENDing_commands.subscriber
and Info.subscriber_db = PENDing_commands.subscriber_db

left outer join msdb..sysjobs jobs

on Info.job_id=jobs.job_id

inner join
(
SELECT time
, agent_id 
,runstatus
,delivery_latency
, comments
,row_number() over ( partition by agent_id order by time desc ) as pos

FROM distribution..MSdistribution_history
)comment

on comment.agent_id = Info.id

where comment.pos =1 ;
GO

答案 2 :(得分:-1)

由于您的表/架构已知,请尝试这种方式。

而不是像你已经完成的那样的表类型变量

DECLARE @tab AS TABLE (pendingcmds int, estimatedprocesstime int)

创建一个临时表,然后在那里插入数据,如

CREATE TABLE #tab (pendingcmds int, estimatedprocesstime int)

然后进行插入,如

INSERT into #tab(pendingcmds,estimatedprocesstime) 
EXEC sp_replmonitorsubscriptionpendingcmds   @publisher=[AUTOSQL1\COMPLOT],  
@publisher_db=PTK_M,  @publication=PTK_M_1way,  
@subscriber=[COMPLOTSQL2008\REPL],  @subscriber_db=
PTK_M,  @subscription_type=0'

选择值

SELECT * FROM #tab