我想在使用此声明启动代理后检查代理的状态
EXEC sp_startpublication_snapshot @publication
因为我想做下一步需要已经开始的工作。
答案 0 :(得分:2)
我不相信有一个内置的复制存储过程来检查快照代理状态,我可能是错的。但是,您可以查询MSsnapshot_history。这样的事情可以解决问题:
SELECT
agent_id,
runstatus,
start_time,
time,
duration,
comments,
delivered_transactions,
delivered_commands,
delivery_rate,
error_id,
timestamp
FROM dbo.MSsnapshot_history
WHERE comments = 'Starting agent.'
同样,您可以检查快照代理何时完成:
SELECT
agent_id,
runstatus,
start_time,
time,
duration,
comments,
delivered_transactions,
delivered_commands,
delivery_rate,
error_id,
timestamp
FROM dbo.MSsnapshot_history
WHERE comments = '[100%] A snapshot of 68 article(s) was generated.'
或者,您可以使用sp_help_job来获取快照代理作业的状态。
答案 1 :(得分:1)
经过一番研究后我得到了一个解决方法
SELECT snapshot_ready FROM sysmergepublications
如果未准备好,则此查询返回0,如果已启动,则返回1
感谢所有人的贡献:)
答案 2 :(得分:0)
您还可以使用while循环在执行其他脚本之前检查代理是否正在运行:
use [distribution];
declare @status int = 2
select @status = status
FROM dbo.MSReplication_monitordata
WHERE publication = 'PublicationName' and agent_type = 1
while @status = 3
begin
WAITFOR DELAY '00:00:03'
select @status = status
FROM dbo.MSReplication_monitordata
WHERE publication = 'Publication.Name' and agent_type = 1
end
参考:T-SQL script to wait till status of snap agent is completed
答案 3 :(得分:0)
我意识到这是一个较旧的线程。但是,由于这仍然很可能是当前版本的SQL(在此更新中为2017/2019)需要完成的工作,因此这是我在Powershell中使用的功能。其目的是坐在快照可用之前,然后在我们的DEV / QA环境中重建复制。
# =======================================================================================================================================
# WAIT UNTIL SNAPSHOT CREATED
# =======================================================================================================================================
Function Wait-ForSnapshotCreation {
[Cmdletbinding()]
Param (
[int] $InitialSleepInSec = 60
,[int] $IncrementalSleepInSec = 2
,[string] $ReplicationPublication
,[string] $ReplicationDistributor
)
Process {
$SleepMessage = "Sleeping for {0} seconds at the start..." -f $InitialSleepInSec
$TotalSleepInSec = $SleepInSec
$MonitorSQL = "exec sp_replmonitorhelppublication @publication = N'$ReplicationPublication'"
Write-Message -Message ($SleepMessage)
Start-Sleep -s $InitialSleepInSec
Do {
$MonitorStatus = (Invoke-SQLCmd -Query ($MonitorSQL) -ServerInstance $ReplicationDistributor -Database 'Distribution' -AbortOnError)
If ($MonitorStatus.status -eq 3) {
$SleepMessage = "Replication status is still 'running' (3). Will sleep for {0} more seconds. Slept {1} seconds so far..." -f $IncrementalSleepInSec, $TotalSleepInSec
Write-Message -Message ($SleepMessage)
Start-Sleep -s $IncrementalSleepInSec
$TotalSleepInSec += $IncrementalSleepInSec
} # If...
} Until ($MonitorStatus.status -ne 3)
} # Process
} # Function
以下是一个示例调用(在创建订阅者之前使用):
Wait-ForSnapshotCreation -InitialSleepInSec 15 -IncrementalSleepInSec 1 -ReplicationPublication '<YOUR_PUBLICATION>' -ReplicationDistributor '<YOUR_DISTRIBUTION_SERVER_INSTANCE>'
答案 4 :(得分:-1)
检查其运行状态。以下是它的价值观: 1 =开始。
2 =成功。
3 =正在进行中。
4 =空闲。
5 =重试。
6 =失败。