Question

I have a SQL Server, which has merge replication configured to around 100 subscriber databases.

I would like to be able to force an ad hoc synchronisation to all the subscribers from the publisher using TSQL.

I have found some code on TechNet which calls out to an executable, but I was hoping to avoid that for security reasons etc.

Any way to do it without?

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

From here: http://technet.microsoft.com/en-us/library/ms147377(v=sql.105).aspx

Was it helpful?

Solution

Found out the answer using EXECUTE msdb.dbo.sp_start_job ....

See below for a cursor that runs through all databases in a supplied list.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top