1

我有一个 SQL Server,它配置了大约 100 个订阅数据库的合并复制。

我希望能够使用 TSQL 对来自发布者的所有订阅者强制进行临时同步。

我在 TechNet 上发现了一些调用可执行文件的代码,但出于安全原因等,我希望避免这种情况。

有没有办法做到这一点?

REM -- Declare the variables.
SET Publisher=%instancename%
SET Subscriber=%instancename%
SET PublicationDB=AdventureWorks2008R2
SET SubscriptionDB=AdventureWorks2008R2Replica 
SET Publication=AdvWorksProductsTran

REM -- Start the Distribution Agent with four subscription streams.
REM -- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\100\COM\DISTRIB.EXE" -Subscriber %Subscriber% 
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication% 
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher% 
-DistributorSecurityMode 1 -Continuous -SubscriptionType 0 -SubscriptionStreams 4

从这里: http ://technet.microsoft.com/en-us/library/ms147377(v=sql.105).aspx

4

1 回答 1

1

使用 EXECUTE msdb.dbo.sp_start_job ... 找到答案。

有关在提供的列表中运行所有数据库的游标,请参见下文。

DECLARE @DatabaseName varchar(50)
DECLARE @Job_Name NVARCHAR(100)

DECLARE @SQL NVARCHAR(max)

DECLARE dc CURSOR FAST_FORWARD READ_ONLY FOR
SELECT UniqueName
FROM mylistofdatabases

OPEN dc
FETCH NEXT FROM dc INTO @DatabaseName

WHILE @@FETCH_STATUS = 0
BEGIN

    --Use MSDB systables to find the new SQL Agent Job ID.
    SELECT TOP 1 @Job_Name =J.name 
    FROM msdb.dbo.sysjobs AS J
    INNER JOIN msdb.dbo.sysjobsteps AS S
    ON S.job_id = J.job_id
    WHERE S.command LIKE '%' + @DatabaseName + '%'
    AND J.name LIKE '%mainjob%'
    ORDER BY J.date_created DESC

    --Kick off the job
    SET @SQL = 'EXECUTE msdb.dbo.sp_start_job N''' + CAST(@Job_Name AS VARCHAR(100)) + ''''
    EXEC(@SQL)

    FETCH NEXT FROM dc INTO @DatabaseName

END

CLOSE dc
DEALLOCATE dc
于 2014-01-28T17:32:07.043 回答